<asp:Repeater ID="RptrSearchedPhotographer" runat="server"
onitemcommand="RptrSearchedPhotographer_ItemCommand"
onitemdatabound="RptrSearchedPhotographer_ItemDataBound">
<ItemTemplate>
<tr>
<td>
<asp:Label ID="LblContactInfo" runat="server" Text='<%# Eval("ContactInfo")%>'/>
<asp:TextBox ID="TxtContactInfo" runat="server" Text='<%#Eval("ContactInfo") %>' Visible="false" ></asp:TextBox>
</td>
<td>
<asp:LinkButton ID="LnkDelete" runat="server" CommandArgument='<%#Eval("Id") %>' CommandName="delete">Delete</asp:LinkButton>
<asp:LinkButton ID="lnkEdit" runat="server" CommandArgument='<%#Eval("Id") %>' CommandName="edit" EnableViewState ="true">Edit</asp:LinkButton>
<asp:LinkButton ID="lnkUpdate" runat="server" CommandArgument='<%#Eval("Id") %>' CommandName="update" Visible="false" EnableViewState="true">Update</asp:LinkButton>
<asp:LinkButton ID="LinkCancel" runat="server" CommandName="cancel" Visible="false">cancel</asp:LinkButton>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
my .cs code is...
if (e.CommandName.Equals("update"))
{
TextBox DetailNote = (TextBox)e.Item.FindControl("txtDetailNote");
string s = DetailNote.Text;
}
But here... It gives the me old value of s from textbox. I want new value which is inserted during run time... I googled a lot... but it doesn't work...
I had this issue, I solved it by adding an "if(!Page.IsPostBack)" before databinding my repeater in the page_load function.
Related
I have a Gridview that has 3 columns, ID, Text, Action. I am using VS2010. When the grid is empty and the user enters text in the text field, I would like the field to be validated as a required field and a max. length. These validations work fine if there is data in the grid but when the grid is empty, the required validation is triggered after the user enters data. If the user enters a second time, the data is successfully added to the database and refreshed in the grid.
Secondly, the column headers will not show when the grid is empty even though I had the attribute: ShowHeaderWhenEmpty="true"
This is my markup:
<asp:GridView ID="SubjectInfoGridView" runat="server"
AutoGenerateColumns="false" Caption="Personal Subject List"
CaptionAlign="Top" CssClass="grid"
RowStyle-Wrap="true" HorizontalAlign="Left" ShowFooter="true"
AllowPaging="false" PageSize="5" ShowHeaderWhenEmpty="true"
onrowcancelingedit="SubjectInfoGridView_RowCancelingEdit"
onrowediting="SubjectInfoGridView_RowEditing"
onrowdeleting="SubjectInfoGridView_RowDeleting"
onrowcommand="SubjectInfoGridView_RowCommand"
onrowupdating="SubjectInfoGridView_RowUpdating">
<Columns>
<asp:TemplateField HeaderText="Subject ID">
<ItemTemplate>
<asp:Label ID="sigvLblSubjectID" runat="server" Text='<%# Bind("SubjectID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Subject" ItemStyle-Wrap="false">
<ItemTemplate>
<asp:Label ID="sigvLblSubject" runat="server" Text='<%# Bind("Subject") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="sigvTxtBoxEditSubject" runat="server" Text='<%# Bind("Subject") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldEditSubject" ControlToValidate="sigvTxtBoxEditSubject" runat="server"
ErrorMessage="Required field." ValidationGroup="EditSubjectValidation" Display="Dynamic" CssClass="message-error">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="MaxValEditSubject" ControlToValidate="sigvTxtBoxEditSubject" runat="server"
ErrorMessage="Maximumn length is 80." ValidationGroup="EditSubjectValidation" Display="Dynamic" CssClass="message-error"
ValidationExpression="^.{1,80}$" >
</asp:RegularExpressionValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="sigvTxtBoxInsertSubject" runat="server" Text='<%# Bind("Subject") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldInsertSubject" ControlToValidate="sigvTxtBoxInsertSubject" runat="server"
ErrorMessage="Required field." ValidationGroup="InsertSubjectValidation" Display="Dynamic" CssClass="message-error">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="MaxValInsertSubject" ControlToValidate="sigvTxtBoxInsertSubject" runat="server"
ErrorMessage="Maximumn length is 80." ValidationGroup="InsertSubjectValidation" Display="Dynamic" CssClass="message-error"
ValidationExpression="^.{1,80}$" >
</asp:RegularExpressionValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Action" ItemStyle-Wrap="false" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Button ID="sigvEditButton" runat="server" CausesValidation="False" CommandName="Edit"
Text="Edit" CssClass="gridActionbutton">
</asp:Button>
<asp:Button ID="sigvDeleteButton" runat="server" CausesValidation="False" CommandName="Delete"
Text="Delete" CssClass="gridActionbutton" OnClientClick="return confirm('Are you sure you want to delete this Device Information?')" >
</asp:Button>
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="sigvUpdateButton" runat="server" CausesValidation="True" ValidationGroup="EditSubjectValidation" CommandName="Update"
Text="Update" CssClass="gridActionbutton"></asp:Button>
<asp:Button ID="sigvCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
Text="Cancel" CssClass="gridActionbutton"></asp:Button>
</EditItemTemplate>
<FooterTemplate>
<asp:Button ID="sigvAddButton" runat="server" CommandName="Add" Text="Add Subject" Width="90%" CausesValidation="true"
CssClass="gridActionbutton" ValidationGroup="InsertSubjectValidation">
</asp:Button>
</FooterTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
<tr>
<td colspan="3" style="text-align:center;">
No User-defined Subjects were found for you. Subjects can be added by clicking the 'Add Subject' Button.
</td>
</tr>
<tr>
<td></td>
<td>
<asp:TextBox ID="sigvTxtBoxInsertSubject" runat="server" Text='<%# Bind("Subject") %>' Width="90%"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldInsertSubjectEmpty" ControlToValidate="sigvTxtBoxInsertSubject" runat="server"
ErrorMessage="Required field." ValidationGroup="InsertSubjectValidation" Display="Dynamic" CssClass="message-error">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="MaxValInsertSubjectEmpty" ControlToValidate="sigvTxtBoxInsertSubject" runat="server"
ErrorMessage="Maximumn length is 80." ValidationGroup="InsertSubjectValidation" Display="Dynamic" CssClass="message-error"
ValidationExpression="^.{1,80}$" >
</asp:RegularExpressionValidator>
</td>
<td>
<asp:Button ID="sigvAddButtonEmpty" runat="server" CommandName="Add" Text="Add Subject" Width="90%" CausesValidation="true"
CssClass="gridActionbutton" ValidationGroup="InsertSubjectValidation">
</asp:Button>
</td>
</tr>
</EmptyDataTemplate>
</asp:GridView>
So, why won't the headers show when the grid is empty and what am I doing wrong with the validation that triggers the RequiredValidation after data is entered in the field?
Thanks!
UPDATE
I solved the 'Header problem' but just adding them to the EmptyDataTemplate. But the validation is still a problem.
I did this using VS2012 at another company and it was not a problem. I tried making the TextBox id different than the Insert ID and it still gives the same error.
I figured it out...
In case anybody else has this problem.
To correct the validation, I removed the text binding (Text='<%# Bind("Subject") %>'). Then I changed the ID for the Subject field to the same as in the Footer when I add a row when there is data. This allows me to only check one control within the RowBound event.
This is the data row in the EmptyDataTemplate:
<tr>
<td></td>
<td>
<asp:TextBox ID="sigvTxtBoxInsertSubject" runat="server" Width="90%"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldInsertSubjectEmpty" ControlToValidate="sigvTxtBoxInsertSubject" runat="server"
ErrorMessage="Required field." ValidationGroup="InsertSubjectValidationEmpty" Display="Dynamic" CssClass="message-error">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="MaxValInsertSubjectEmpty" ControlToValidate="sigvTxtBoxInsertSubject" runat="server"
ErrorMessage="Maximumn length is 80." ValidationGroup="InsertSubjectValidationEmpty" Display="Dynamic" CssClass="message-error"
ValidationExpression="^.{1,80}$" >
</asp:RegularExpressionValidator>
</td>
<td>
<asp:Button ID="sigvAddButtonEmpty" runat="server" CommandName="Add" Text="Add Subject" Width="90%" CausesValidation="true"
CssClass="gridActionbutton" ValidationGroup="InsertSubjectValidationEmpty" >
</asp:Button>
</td>
</tr>
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.
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
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.
I have an ASP.NET GridView that uses an EmptyDataTemplate. This template is used to collect data in the event that no records exist in my data source. My GridView source looks like this:
<asp:GridView ID="myGridView" runat="server"
DataKeyNames="ID" OnRowEditing="myGridView_RowEditing"
OnRowCancelingEdit="myGridView_RowCancelingEdit"
OnRowUpdating="myGridView_RowUpdating"
ShowFooter="True" EnableModelValidation="True">
<Columns>
<asp:BoundField DataField="ID" Visible="false" />
<asp:TemplateField HeaderText="Name">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="nameFooterTextBox" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Age">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Age") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Age") %>' />
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="ageTextBox" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="Options" HeaderStyle-HorizontalAlign="Left"
ButtonType="Link" ShowEditButton="true" ShowDeleteButton="true" >
</asp:CommandField>
</Columns>
<EmptyDataTemplate>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>Name</td>
<td>Age</td>
<td>Options</td>
</tr>
<tr>
<td><asp:TextBox ID="nameTextBox" runat="server" /></td>
<td><asp:TextBox ID="ageTextBox" runat="server" /></td>
<td><asp:LinkButton ID="saveLinkButton" runat="server" Text="save" OnClick="saveLinkButton_Click" /></td>
</tr>
</table>
</EmptyDataTemplate>
When a user clicks the "saveLinkButton" in the EmptyDataTemplate, I want to get the values from the TextBoxes and insert a new record into my data source. My question is, how do I get the values of those text fields when somebody clicks the "saveLinkButton"?
Thank you!
This thread over at asp.net proposes a solution to this problem (Note: I haven't tried it out)
http://forums.asp.net/p/1436652/3240106.aspx
You need to handle the RowCommand event, get the parent naming container of the control that raise the event (your linkbutton) and then search for the textboxes using FindControl within that.
Try this to get the values of the name and age in row command of the grid..
Before doing this set the command name of the saveLinkbutton to MyInsert and remove the onlcik event as u may not need it.
protected void yourGridview_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("MyInsert"))
{
TextBox nameTextBox= gvEligibility.Controls[0].Controls[0].FindControl("nameTextBox") as TextBox;
TextBox ageTextBox= gvEligibility.Controls[0].Controls[0].FindControl("ageTextBox") as TextBox;
string name=nameTextBox.Text();
string age=ageTextBox.Text();
//save code here
}
}