问题
I have a DetailsView Control, inside as a template a TextBox. I need to find out the value for a TextBox when Inserting data Event handler-, _ItemInserting.
The script does not work. Ant ideas?? Thanks
-------------------- WEB FORM
<asp:TemplateField HeaderText="Profile" SortExpression="ContentAuthor">
<ItemTemplate>
<asp:Label ID="uxContentAuthorDisplayer" runat="server" Text='<%# Bind("ContentAuthor") %>'></asp:Label>
</ItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="uxContentAuthorInput" runat="server" Text='<%# Bind("ContentAuthor") %>'></asp:TextBox>
</InsertItemTemplate>
</asp:TemplateField>
-------------------- CODE BEHIND
protected void uxInsertAuthor_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
//// Find control on page
TextBox myAuthorProfile = (TextBox)uxInsertAuthorInput.FindControl("uxContentAuthorDisplayer");
// Set a default value in Data Base if field has been left empty (DB field NOT NULL)
if (string.IsNullOrEmpty(myAuthorProfile.Text))
{
string myAllert = "Field is NULL";
}
else
{
string myAllet = "Field is NOT NULL";
}
}
回答1:
Try using the FindControl
method of your TemplateField
container control (the DetailsView). For example, if your DetailsView is named "MyControl" try
//// Find control on page
TextBox myAuthorProfile = (TextBox)MyControl.FindControl("uxContentAuthorInput");
Please Note
You are using the ID of the Label control in your FindControl method and you are trying to cast it to a TextBox.
回答2:
Another option is to get the value from the DetailsViewInsertEventArgs. It has a property called Values
which is a dictionary with all the bound values that will go into the database from the DetailsView. So you could do the following instead:
if (string.IsNullOrEmpty(e.Values("ContentAuthor")))
{
string myAlert = "Field is NULL";
}
else
{
string myAlert = "Field is NOT NULL";
}
回答3:
If I understood it correctly, you want to check the value of the TextBox
when the data is put into the DetailsView
. The ItemInserting
event is not the one you're looking for:
http://msdn.microsoft.com/en-us/library/keezbt7k.aspx
I'd say ItemCreated
is the one you're looking for.
来源:https://stackoverflow.com/questions/4132795/how-to-retrieve-a-textbox-from-detailsview-asp-net