Reputation: 171
I am new to ASP.NET. I have been trying all morning, but couldn't get this to work. In my DataList1, there is a child dataList (childList), which lists the Company names and checkboxes. I also stored the CompanyID in the hidden field (hiddenCompanyID).
What I like to do is when the user checkes a boxes (under child Datalist), it will trigger the event (chkChildCompany_CheckedChanged) and capture the CompanyID from the hidden field so I can process the database update. I could not figure how to find the Hidden field control with in the child Datalist.
Please help, Thanks,
<asp:DataList BackColor="#ffffff" id="DataList1" DataSourceID="dsCompanyListPartialMatch" runat="server" Width="80%" DataKeyField="Company1Word"
UseAccessibleHeader="true"
CssClass="books"
HeaderStyle-CssClass="header"
ItemStyle-CssClass="item"
AlternatingItemStyle-CssClass="alternating"
GridLines="Both"
CellPadding="0"
CellSpacing="0" BorderColor="Black"
ItemStyle-BorderColor="Black" BorderWidth="0"
HorizontalAlign="Center"
RepeatDirection="Vertical"
>
<HeaderTemplate>
<table border="0" width="100%">
<tr class="div_hover">
<th style="width: 5%; border-right:1px solid black; border-spacing:0; text-align:center; "></th>
<th style="width: 5%; border-right:1px solid black; border-spacing:0; text-align:center; ">Num</th>
<th style="width: 70%; border-right:1px solid black; border-spacing:0; text-align:center; ">Company Name</th>
<th style="width: 10%; border-right:1px solid black; border-spacing:0; text-align:center; ">Add?</th>
</tr>
</table>
</HeaderTemplate>
<ItemStyle BorderColor="black" Font-Size="Medium" />
<ItemTemplate>
<table border="0" width="100%">
<tr class="div_hover">
<td style="width: 5%; border-right:1px solid black; border-spacing:0; text-align:center; ">
<asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" Text="+" CommandArgument='<%#Container.ItemIndex%>'
OnCommand="LinkButton1_Command"
Font-Underline="false"
Height="25"
Font-Bold="true"
></asp:LinkButton>
</td>
<td style="width: 5%; border-right:1px solid black; border-spacing:0; text-align:right; padding-right:10px;"><%#Eval("Row")%></td>
<td style="width: 70%"><asp:Literal ID="litFoo" runat="server" Text='<%#Eval("Company")%>' /> </td>
<td style="width: 10%;text-align:right;">
<div style="<%# (Eval("CompanyID") == null || Eval("CompanyID").ToString() == "") ? "": "display: none" %>">
<asp:CheckBox id="check1" runat="server" />
</div>
<div style="<%# (Eval("CompanyID") == null || Eval("CompanyID").ToString() != "") ? "": "display: none" %>">
<asp:HyperLink
ID="HyperLink1"
runat="server" ForeColor="Blue"
Text='<%# Eval("CompanyID")%>'
NavigateUrl='<%# Eval("CompanyID", "/Apps/ERP/Other/CompanyInfo.asp?CompanyID={0}")%>'
/>
</div>
</td>
<asp:Label ID="lblRow" Visible="False" runat="Server" Text='<%# DataBinder.Eval(Container.DataItem, "Row") %>' />
</tr>
</table>
<asp:Panel ID="pnlChildView" runat="server" style="padding-left:200px;">
<asp:DataList ID="childList" runat="server" Width="100%">
<ItemTemplate>
<div class="div_hover">
<table class="table1" width="80%">
<tr>
<td style="width: 60%; border-right:0px solid black; border-spacing:0;">• <%#Eval("CompanyName")%></td>
<td style="width: 20%;text-align:right; "><a href="/Apps/ERP/Other/CompanyInfo.asp?CompanyID=<%#Eval("CompanyID")%>" ><%#Eval("CompanyID")%></a></td>
<td style="width: 20%;text-align:right;">
<asp:CheckBox id="chkChildCompany" runat="server" value="123Test"
AutoPostBack="true"
OnCheckedChanged="chkChildCompany_CheckedChanged" /></td>
<asp:HiddenField ID="hiddenCompanyID" runat="server" Value='<%#Eval("CompanyID") %>' />
</tr>
</table>
</div>
</ItemTemplate>
</asp:DataList>
</asp:Panel>
</ItemTemplate>
</asp:DataList>
checkbox event
protected void chkChildCompany_CheckedChanged(object sender, EventArgs e)
{
//Process through child DataList (childList)
//Capture the CompanyID from hidden field value if checkbox is checked;
// Then I will update the database record with the ID....
}
Upvotes: 0
Views: 2427
Reputation: 1145
I would add an attribute to your checkbox, and then obtain the value you need from the checkbox itself.
<asp:CheckBox id="chkChildCompany" runat="server" value="123Test"
AutoPostBack="true" CustomAttribute='<%#Eval("CompanyID") %>'
OnCheckedChanged="chkChildCompany_CheckedChanged" />
CodeBehind:
var CompanyID = (sender as CheckBox).Attributes["CustomAttribute"]
Upvotes: 1
Reputation: 3280
In your case, you should store the CompanyID in ViewState instead of a hidden field:
ViewState["MicrosoftID"] = "4536";
Later, you can access it like this:
protected void chkChildCompany_CheckedChanged(object sender, EventArgs e)
{
string companyID = ViewState["MicrosoftID"];
}
Hope that helps!
Upvotes: -1
Reputation: 8788
A couple different ways to tackle this. My first instinct is to cast sender to the appropriate type and then check the .Parent property. That will probably allow you to do this:
HiddenField hidden = (HiddenField) myCheckbox.Parent.FindControl("hiddenCompanyID");
If that doesn't work... another option would be to embed a postback reference that already includes the appropriate hidden field value, which is triggered by the checkbox. Or modify things slightly so that you've got a button there, which accepts a CommandArgument param. In this case you'd just set it to <%#Eval("CompanyID") %>, and then that would be available in the event handler.
Upvotes: 1
Reputation: 460208
You can use the NamigContainer
property to get the DataListItem
, then use FindControl
:
protected void chkChildCompany_CheckedChanged(object sender, EventArgs e)
{
var chk = (CheckBox) sender;
var item = (DataListItem) chk.NamingContainer;
var hiddenCompanyID = (HiddenField) item.FindControl("hiddenCompanyID");
// here you are ...
}
By the way, this technique works in all web-databound controls like Repeater
, GridView
etc. Using the NamingContainer
is safer than using Parent.Parent
or other approaches.
Upvotes: 0