asp.net binding problem - asp.net

I have in my code behind the following property
public string Firstname {get;set;}
when I want to bind it to some textbox I do the following:
<asp:TextBox runat="server" ID="txtFirstname" Text='<%# Bind("Firstname") %>'/>
then I want value put in this textbox to be set in my Firstname property (because I want to process it e.g. save this value) in my presenter.
Why it doesn't work?
EDIT
Here is the aspx
<formview runat="server" ID="myFormView">
<p>Firstname <asp:TextBox ID="txtFirstName" runat="server" Text='<%# Eval("Firstname") %>' /></p>
<p>Lastname <asp:TextBox ID="txtLastName" runat="server" /></p>
<input type="button" title="send" runat="server" id="btnSend" />
</formview>

It will bind in the page load but you have to tell it what to bind to in mark up or in code. You didn't say where or how you're storing your data and it sounds like you are trying to insert new data so...
Here is a tutorial on the sqldatasource.
SQL Datasource Tutorial
Here is a turorial on the formview:
Formview Tutorial
Here is a simple one I whipped up...(NOTE: I did not test the below code, so if I forgot somehting my apologies, but it should give you a good start).
<asp:SqlDataSource ID="SqlDataSource1"
runat="server"
ConnectionString="Connection string for your database here."
SelectCommand="SELECT FirstName, LastName FROM YourTable"
>
</asp:SqlDataSource>
<asp:FormView ID="frmYourForm" DefaultMode="Insert" runat="server" DataSourceID="SqlDataSource1">
<EditItemTemplate>
<asp:TextBox ID="txtFirstName" runat="server" Text='<%# Bind("FirstName") %>'></asp:TextBox>
<br />
<asp:TextBox ID="txtLastName" runat="server" Text='<%# Bind("LastName") %>'></asp:TextBox>
<asp:LinkButton ID="LinkButton1" CommandName="Update" runat="server">Update</asp:LinkButton>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="txtFirstName" runat="server" Text='<%# Bind("FirstName") %>'></asp:TextBox>
<br />
<asp:TextBox ID="txtLastName" runat="server" Text='<%# Bind("LastName") %>'></asp:TextBox>
<asp:LinkButton ID="LinkButton1" CommandName="Insert" runat="server">Insert</asp:LinkButton>
</InsertItemTemplate>
</asp:FormView>
EDIT: Fixed the links to the tutorials...I didn't realize the orginal link didn't show info on the formview.

Related

Issues with a LinkButton not fireing when turning OFF compatility view IE11

I built a.net Web Application with the compatibility view activated on IE without knowing. Deactivating the compatibility view not just makes everything look like crap, but also cuts the functionality of a LinkButton that is not firing the event.
I'm building a calendar by populating a repeater in a sequential way. The Data comes from a SQL.
<asp:Repeater runat="server" id="RepeaterCalendar">
<ItemTemplate>
<asp:label ID="Month_lbl" runat="server" Text='<%# Bind("Month") %>'/>
<asp:label ID="Pretag_lbl" runat="server" Text='<%# Bind("PRETAG") %>'/>
<asp:LinkButton ID="ButtonSelect" runat="server" CommandName ="Select" CommandArgument = '<%# Eval("date") %>' Text='<%# Bind("TAG") %>' style="vertical-align:text-bottom" OnClick="GetDetails" Visible="true" />
<asp:label ID="Posttag_lbl" runat="server" Text='<%# Bind("POSTTAG") %>'/>
<asp:label ID="Monthend_lbl" runat="server" Text='<%# Bind("MonthEnd") %>'/>
</ItemTemplate>
</asp:Repeater>
With Compatibility view turned off the LinkButton doesn't fire the event, while it does work perfectly when switching Compatibility view on again.
I guess it has to do with all the fractured code bits that I glue together to form the calendar table using the asp:repeater. If I bind a "normal" column without any html tags, then the LinkButton works as expected; but this ruins the whole calendar design.
Martin
<asp:Repeater runat="server" id="RepeaterCalendar">
<ItemTemplate>
<asp:label ID="Month_lbl" runat="server" Text='<%# Bind("Month") %>'/>
<asp:label ID="Pretag_lbl" runat="server" Text='<%# Bind("PRETAG") %>'/>
<asp:LinkButton ID="ButtonSelect" runat="server" CommandName ="Select"
CausesValidation="false" CommandArgument = '<%# Eval("date") %>' Text='<%# Bind("TAG") %>' style="vertical-align:text-bottom" OnClick="GetDetails" Visible="true" />
<asp:label ID="Posttag_lbl" runat="server" Text='<%# Bind("POSTTAG") %>'/>
<asp:label ID="Monthend_lbl" runat="server" Text='<%# Bind("MonthEnd") %>'/>
</ItemTemplate>
</asp:Repeater>
Use CausesValidation="false"

Confusion about Retrieve image from database

I am trying to retrieve images to gridview. But My images not displaying .I can get all the data except images. I keep the their paths in the database and I have a folder named Resimler stores images .I have been trying to figure out three hours and i dont understand what is missing or wrong This is my code
<div>
<asp:GridView ID="GvSehirBilgileri" runat="server" AutoGenerateColumns="False"
DataSourceID="sql"
onrowdatabound="GvSehirBilgileri_RowDataBound"
Height="327px" Width="376px">
<Columns>
<asp:TemplateField>
<ItemTemplate>
CityName :<asp:Label ID="lblSehirAdi" runat="server"
Text='<%# Bind("CityName") %>'></asp:Label>
<br />
TaksiPrice :<asp:Label ID="Label2" runat="server"
Text='<%# Bind("TaxiPrice") %>'></asp:Label>
<br />
Resim :
<asp:Image ID="imgpath" runat="server"
ImageUrl='<%#Bind("Path") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="sql" runat="server"
ConnectionString="<%$ ConnectionStrings:SQL %>"
SelectCommand="SELECT * FROM [locCities]"></asp:SqlDataSource>
<asp:Button ID="Button1" runat="server"
onclick="Button1_Click" Text="Button" />
<asp:TextBox ID="txtID" runat="server"></asp:TextBox>
</div>
Set ImageUrl like this:
ImageUrl='<%# string.Format("~/{0}", Eval("Path")) %>'

Changing AccessDataSource to SqlDataSource

I'm trying to change my database from Access to MS SQL and I've got the database ready and everything. I've gone around my website and changed the IDs for each data source to SqlDataSource(number here) and then changed each control that uses the property "DataSourceID" to reflect the name change. However, when I try to open my website, it says it is still looking for AccessDataSource1. Here is some of my code:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:TravelJoansDBSQLConnectionString %>"
SelectCommand="SELECT TOP 8 * FROM [Table2]"></asp:SqlDataSource>
<asp:DataList ID="DataList1" DataSourceID="SqlDataSource1"
runat="server" ItemStyle-CssClass="picTableStyle"
DataKeyField="ID">
<ItemStyle></ItemStyle>
<ItemTemplate>
<br /><br />
<table>
<tr>
<td>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%#"~/Places.aspx?ID="+Eval("ID")%>'>
<asp:Image ID="Image1" runat="server" BorderColor="#CCCCCC" BorderWidth="1px" Width="220px" Height="180px" ImageUrl='<%# "PlaceImages/" + Eval("Image") %>' /><br /></asp:HyperLink>
<asp:Label ID="Label1" CssClass="picCaptionStyle" runat="server" Text='<%# Eval("Caption") %>' /></td>
<td>
<asp:HyperLink runat="server" NavigateUrl='<%#"~/Places.aspx?ID="+Eval("ID")%>'>
<asp:Label ID="Label2" class="picTitleStyle" runat="server" Text='<%# Eval("BlogTitle") %>' /><br /></asp:HyperLink>
<asp:Label ID="Label3" class="picBodyText" runat="server" Text='<%# Eval("TravelDate", "{0:MMMMMMMMM dd, yyyy}") %>' /><br /><br />
<asp:Label ID="Label4" class="picBodyText" runat="server" Text='<%# Limit(Eval("PicText"), 400)+"..." %>' />
<asp:LinkButton ID="LinkButton1" runat="server" PostBackUrl='<%#"~/Places.aspx?ID="+Eval("ID")%>' Text="[Read More]" />
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
Again, when I try to run my website based on this code, I get the error "The DataSourceID of 'DataList1' must be the ID of a control of type IDataSource. A control with ID 'AccessDataSource1' could not be found." Any help would be much appreciated.
Try a clean then rebuild. You can also try a Ctrl+F in all files to see if "AccessDataSource1" is lingering somewhere.

ASP.NET - Get control in RowCommand event

Trying to access a RequiredFieldValidator control that's inside a GridView in the RowCommand event and having trouble.
Here's the partial GridView code:
<asp:TemplateField HeaderText="Password">
<ItemTemplate>
<asp:TextBox ID="txtPassword" runat="server" CssClass="GridViewTextbox" TextMode="Password" Text='<%#Eval("WebPassword") %>' Enabled="false"></asp:TextBox>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtWebPassword" runat="server" TextMode="Password" Text='<%#Eval("WebPassword") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvPassword" runat="server" SetFocusOnError="true"
ControlToValidate="txtWebPassword" Display="None" ErrorMessage='<%# Constants.Strings.PasswordRequired %>'></asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtAddWebPassword" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvAddPassword" runat="server" SetFocusOnError="true"
ControlToValidate="txtAddWebPassword" Display="None" ErrorMessage='<%# Constants.Strings.PasswordRequired %>'></asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
As you can see, there's a RFV for the EditTemplate and FooterTemplate. My issue is this; when the page loads, it has all the records in it, including an emtpy row at the bottom (Footer). If I click Edit on a populated row, the data is populated correctly, then when I hit UPDATE, I get all the error messages from the FOOTER RFV's firing off, which isn't correct. So, in the RowCommand event, I'd like to attempt this: If the user clicks the EDIT button, then disable all the RFV's in the footer row (the add new row), if they click anything else, enable them.
Ideas?
Sorry, meant to put this in the first time. In the RowCommand event, I am able to find the control but when I set the properties to something bogus, it seems to get overridden later, by the RowDataBound event:
RequiredFieldValidator rfv = (RequiredFieldValidator)gvUsers.FooterRow.FindControl("rfvAddWebLogin");
rfv.ControlToValidate = string.Empty;
rfv.ErrorMessage = "sdfgsdfgsdgsdfgsdfgsdfgsdfg";
rfv.Enabled = false;
You should use different ValidationGroups in your EditItemTemplate and FooterItemplate:
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:Button ID="BtnEdit" CausesValidation="False" Text="Edit" CommandName="Edit" runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="BtnUpdate" ValidationGroup="UpdateUser" Text="Update" CommandName="Update" runat="server" />
</EditItemTemplate>
<FooterTemplate>
<asp:Button ID="BtnInsert" ValidationGroup="InsertUser" Text="Add" CommandName="Insert" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Password">
<ItemTemplate>
<asp:TextBox ID="txtPassword" runat="server" CssClass="GridViewTextbox" TextMode="Password"
Text='<%#Eval("WebPassword") %>' Enabled="false"></asp:TextBox>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtWebPassword" ValidationGroup="UpdateUser" runat="server" TextMode="Password" Text='<%#Eval("WebPassword") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvPassword" ValidationGroup="UpdateUser" runat="server" SetFocusOnError="true"
ControlToValidate="txtWebPassword" Display="None" ErrorMessage='<%# Constants.Strings.PasswordRequired %>'></asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtAddWebPassword" ValidationGroup="InsertUser" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvAddPassword" ValidationGroup="InsertUser" runat="server" SetFocusOnError="true"
ControlToValidate="txtAddWebPassword" Display="None" ErrorMessage='<%# Constants.Strings.PasswordRequired %>'></asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
http://msdn.microsoft.com/en-us/library/bb426882.aspx#aspnett19_vldcntredtuics_topic7
Note: If you're using ValidationSummaries, you need to add the appropriate ValidationGroup to every ValidationSummary. If you leave this property blank, only controls without a specified ValidationGroup will be listed.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.validationsummary.validationgroup.aspx

ASP.NET AJAX Control Toolkit ValidatorCallout not working on edit template

I have a page that has a listview that is used for inserting and editing records.
Assigning a RequiredFieldValidator and ValidatorCallOutExtender to the InsertItemTemplate works well.
When I try to do the same on the EditItemTemplate the ValidatorCallOut appears but with no text in the box.
Is there something that I'm doing wrong?
My code for the InsertItemTemplate:
<asp:TextBox ID="date_timeTextBox" runat="server" Text='<%# Bind("date_time") %>' />
<asp:RequiredFieldValidator
ControlToValidate="date_timeTextBox"
ID="RequiredFieldValidator1"
runat="server"
ErrorMessage="date_time is required"
Display="None"
ValidationGroup="insert_into">
</asp:RequiredFieldValidator>
<cc1:ValidatorCalloutExtender ID="ValidatorCalloutExtender1"
runat="server"
TargetControlID="RequiredFieldValidator1">
</cc1:ValidatorCalloutExtender>
And for the EditItemTemplate:
<asp:TextBox
ID="date_timeTextBox"
runat="server"
Text='<%# Bind("date_time","{0:yyyy-MM-dd}") %>' />
<asp:RequiredFieldValidator
ControlToValidate="date_timeTextBox"
ID="reqDTT"
runat="server"
ErrorMessage="date_time is required"
Display="None"
ValidationGroup="edit_validate">
</asp:RequiredFieldValidator>
<cc1:ValidatorCalloutExtender
ID="val_reqDTT"
runat="server"
TargetControlID="reqDTT">
</cc1:ValidatorCalloutExtender>
Make sure your ID's are unique across your Templates, so the ControlToValidate="date_timeTextBox" is different.
InsertTemplate
<asp:TextBox ID="date_timeTextBoxInsert" runat="server" Text='<%# Bind("date_time") %>' />
EditTemplate
<asp:TextBox ID="date_timeTextBoxEdit" runat="server" Text='<%# Bind("date_time") %>' />

Resources