I like to know how to get the ClientID/UniqueID of a control inside a Detailsview controls EditItemTemplate element and when DetailsViews changing to Edit mode and DetailsView is inside a AJAX UpdatePanel. Without UpdatePanel, during PostBack I can get the ClientID's control, but now with an UpdatePanel.
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:DetailsView ID="DetailsView1" runat="server" DataSourceID="SqlDataSource1" AllowPaging="true" AutoGenerateEditButton="true">
<Fields>
<asp:TemplateField>
<EditItemTemplate>
<asp:CheckBox runat="server" ID="chkboxTest" Text="CHECKBOX" />
</EditItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
</ContentTemplate>
</asp:UpdatePanel>
As you see, the EditItemTemplate contains a Checkbox control. So i'm trying to get the ClientID of this checkbox when Detailsview is changing to the Edit mode. I need this value for handling Javascript.
Catching the events ChangingMode/ChangedMode doesn't work; chkbox is null:
void DetailsView1_ModeChanged(object sender, EventArgs e)
{
if (DetailsView1.CurrentMode == DetailsViewMode.Edit)
{
var chkbox = DetailsView1.FindControl("chkboxTest"); // <== is null
}
}
Maybe i'm using the wrong event? Someone can give me a tip about this? Thanks开发者_开发知识库.
Ok, the best thing to do is implement a handler for OnDataBound, then do something like:
protected void databound(object sender, EventArgs e)
{
if (DetailsView1.CurrentMode == DetailsViewMode.Edit)
{
var control = DetailsView1.Rows[0].Cells[1].FindControl("chkboxTest");
if (control != null)
{
// Write some JS...
}
}
}
void DetailsView1_ModeChanged(object sender, EventArgs e) { if (DetailsView1.CurrentMode == DetailsViewMode.Edit) var chkbox = DetailsView1.Rows[0].FindControl("chkxboxTest"); // <== is null }
Is the enboldened text a typo?
I haven't had much use of the DetailsView but make usre Rows[0] is not a header row, and are there any Cells under the rows? Like the GridView.
UPDATE: I'm assuming all you want to do is capture the control after the user has updated the items? Assign an event handler to OnItemUpdating and try the following:
protected void updating(object sender, DetailsViewUpdateEventArgs e)
{
var control = DetailsView1.Rows[int.Parse(e.CommandArgument.ToString())].Cells[1].FindControl("chkboxTest");
}
精彩评论