how to find checked radio button in a gridview? - c#

how to find checked radio button?
there is a hatml input with type of radio and has 4 options to select called o1 o2 o3 and o4.
i can access the radio buttons with no problem.
how should i check which option is selected?
<asp:GridView OnRowCommand="SelectedPollGridView_RowCommand" ID="SelectedPollGridView" runat="server" AutoGenerateColumns="False" DataKeyNames="PollID" DataSourceID="SelectedPollSqlDataSource">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<p runat="server" id="HeaderPTag" class="text-center"><small><%#Eval("Header") %></small></p>
</HeaderTemplate>
<ItemTemplate>
<p runat="server" id="BodyPTag" class="text-right"><%#Eval("Body") %></p>
<asp:Label Visible="false" ID="PollIDLabel" runat="server" Text='<%#Eval("PollID") %>'></asp:Label>
<div runat="server" id="MainDiv">
<div runat="server" id="O1Div">
<label runat="server" id="O1Label">
<input runat="server" type="radio" name="OptionsOne" id="O1" value='<%#Eval("PollID") %>'>
<%#Eval("O1") %>
</label>
</div>
<div runat="server" id="O2Div">
<label runat="server" id="O2Label">
<input runat="server" class="pull-right" type="radio" name="OptionsTwo" id="O2" value='<%#Eval("PollID") %>'>
<%#Eval("O2") %>
</label>
</div>
<div runat="server" id="O3Div">
<label runat="server" id="O3Label">
<input runat="server" class="pull-right" type="radio" name="OptionsThree" id="O3" value='<%#Eval("PollID") %>'>
<%#Eval("O3") %>
</label>
</div>
<div runat="server" id="O4Div">
<label runat="server" id="O4Label">
<input runat="server" class="pull-right" type="radio" name="OptionsFour" id="O4" value='<%#Eval("PollID") %>'>
<%#Eval("O4") %>
</label>
</div>
</div>
<asp:Button CommandArgument='<%# ((GridViewRow) Container).RowIndex %>' CommandName="foo" CssClass="btn btn-info" ID="SubmitPollButton" runat="server" Text="ثبت نظر" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SelectedPollSqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:GUOTSConnectionString %>" SelectCommand="SELECT DISTINCT [PollID], [Header], [Body], [O1], [O1Vis], [O2], [O2Vis], [O3], [O1Cnt], [O2Cnt], [O3Cnt], [O3Vis], [O4], [O4Cnt], [O4Vis], [PollDate] FROM [Poll] ">
<SelectParameters>
<asp:QueryStringParameter Name="PollID" QueryStringField="PollID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
and im using this code to access it:
protected void SelectedPollGridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "foo")
{
// Convert the row index stored in the CommandArgument
// property to an Integer.
int index = Convert.ToInt32(e.CommandArgument);
// Retrieve the row that contains the button clicked
// by the user from the Rows collection.
GridViewRow row = SelectedPollGridView.Rows[index];
System.Web.UI.HtmlControls.HtmlInputRadioButton O1Radio = (System.Web.UI.HtmlControls.HtmlInputRadioButton)row.FindControl("O1");
System.Web.UI.HtmlControls.HtmlInputRadioButton O2Radio = (System.Web.UI.HtmlControls.HtmlInputRadioButton)row.FindControl("O2");
System.Web.UI.HtmlControls.HtmlInputRadioButton O3Radio = (System.Web.UI.HtmlControls.HtmlInputRadioButton)row.FindControl("O3");
System.Web.UI.HtmlControls.HtmlInputRadioButton O4Radio = (System.Web.UI.HtmlControls.HtmlInputRadioButton)row.FindControl("O4");
Label myPollIDLAbel = (Label)row.FindControl("PollIDLabel");
}
}
now how should i check which radio button has selected?
thank you very much.

HtmlInputRadioButton has a properties names Checked (return boolean type), you could use this prop. to check which radio button has selected.
For sample, after you get the radio button control in RowCommand event handler, then you have to check the prop. like this:
System.Web.UI.HtmlControls.HtmlInputRadioButton O1Radio = (System.Web.UI.HtmlControls.HtmlInputRadioButton)row.FindControl("O1");
System.Web.UI.HtmlControls.HtmlInputRadioButton O2Radio = (System.Web.UI.HtmlControls.HtmlInputRadioButton)row.FindControl("O2");
System.Web.UI.HtmlControls.HtmlInputRadioButton O3Radio = (System.Web.UI.HtmlControls.HtmlInputRadioButton)row.FindControl("O3");
System.Web.UI.HtmlControls.HtmlInputRadioButton O4Radio = (System.Web.UI.HtmlControls.HtmlInputRadioButton)row.FindControl("O4");
if(O1Radio.Checked)
{
//O1Radio is selected.
}
else if(O2Radio.Checked)
{
//O2Radio is selected.
}
else if(O3Radio.Checked)
{
//O3Radio is selected.
}
else if(O4Radio.Checked)
{
//O4Radio is selected.
}
EDIT
To group radiobuttons, you should set the same name for all radiobuttons in a group:
...
<input runat="server" type="radio" name="Options" id="O1" value='<%#Eval("PollID") %>' />
...
<input runat="server" type="radio" name="Options" id="O2" value='<%#Eval("PollID") %>' />
...
<input runat="server" type="radio" name="Options" id="O3" value='<%#Eval("PollID") %>' />
...
<input runat="server" type="radio" name="Options" id="O4" value='<%#Eval("PollID") %>' />
...

I had a similar kind of situation some time back, that i solved using the below logic.
for (int i = 0; i < myGrid.Rows.Count; i++) //Check if item is selected
{
if (((CheckBox)myGrid.Rows[i].FindControl(cbname)).Checked) //If selected
{
.... //Magic Happens
}
}
So all the rows have checkbox in the grid, and the loop iterates through all the data and checks if the row is selected. Hope it helps :)
Khizer Jalal

Related

How to get image from GridView using GridView_RowCommand event in c# ASP.NET

i am doing edit operation inside GridView using c# ASP.NET.i need when user will click on edit button all data will retrive from that row and display in text box but here i am unable to display the image.I am explaining my code below.
faq.aspx:
<div class="row">
<div class="col-md-6">
<label for="question" accesskey="T"><span class="required">*</span> Question</label>
<asp:TextBox ID="TextBox1" runat="server" size="30" value="" name="question" ></asp:TextBox>
<div id="noty" style="display:none;" runat="server"></div>
<label for="answer" accesskey="A"><span class="required">*</span> Answer</label>
<asp:TextBox ID="TextBox2" runat="server" size="30" value="" name="answer" ></asp:TextBox>
<div id="Div1" style="display:none;" runat="server"></div>
</div>
<div class="col-md-6 bannerimagefile">
<label for="insertimage" accesskey="B"><span class="required">*</span> Insert Image</label>
<asp:FileUpload runat="server" class="filestyle" data-size="lg" name="insertimage" id="FileUpload1" onchange="previewFile()" />
<label for="bannerimage" accesskey="V"><span class="required">*</span> View Image</label>
<div style="padding-bottom:10px;">
<asp:Image ID="Image3" runat="server" border="0" name="bannerimage" style="width:70px; height:70px;" />
</div>
<div class="clear"></div>
<asp:Button ID="Button1" runat="server" Text="Submit" class="submit"
onclick="Button1_Click" />
</div>
</div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" Width="100%" CssClass="table table-striped table-bordered margin-top-zero" OnRowCommand="GridView1_RowCommand" >
<Columns>
<asp:TemplateField HeaderText="Sl No">
<ItemTemplate>
<asp:Label ID="faqid" runat="server" Text='<%#Eval("FAQ_ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Question" >
<ItemTemplate>
<asp:Label ID="question" runat="server" Text='<%#Eval("Question") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Answer" >
<ItemTemplate>
<asp:Label ID="answer" runat="server" Text='<%#Eval("Answer") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Image" >
<ItemTemplate>
<asp:Image ID="Image1" runat="server" border="0" name="bannerimage" style="width:70px; height:70px;" ImageUrl='<%# "/Upload/" + Convert.ToString(Eval("Image")) %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Action" >
<ItemTemplate>
<!--</i>
<i class="fa fa-times"></i> -->
<asp:HyperLink ID="HyperLink1" runat="server" data-toggle="tooltip" title="" class="btn btn-xs btn-success" data-original-title="Edit" CommandName="DoEdit" CommandArgument='<%# Eval("FAQ_ID") %>' ><i class="fa fa-edit"></i></asp:HyperLink>
<asp:HyperLink ID="HyperLink2" runat="server" data-toggle="tooltip" title="" class="btn btn-xs btn-danger" data-original-title="Delete" CommandName="DoDelete" CommandArgument='<%# Eval("FAQ_ID") %>' ><i class="fa fa-times"></i></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
faq.aspx.cs:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
int faqID = int.Parse(e.CommandArgument.ToString());
switch (e.CommandName)
{
case "doEdit":
{
int index = Convert.ToInt32(e.CommandArgument);
TextBox1.Text = GridView1.Rows[index].Cells[1].Text;
TextBox2.Text = GridView1.Rows[index].Cells[2].Text;
HiddenField1.Value = GridView1.Rows[index].Cells[0].Text;
Image3.ImageUrl=
Button1.Text = "Update";
}
}
}
Here i need the image will retrive and set to image3 id.Please help me to solve this issue.
WHere is image3; it's not in your snippet. Please include that, but if it's in the grid, then you do:
var img3 = (Image)GridView1.Rows[index].Cells[X].FindControl("Image3");
img3.ImageUrl = "XYZ";
Direct references only work when on the page outside of a container. If inside a container, you have to use FindControl (such as detailsview.findcontrol) or if a repeatable list, you have to use FindControl from the row (or in the case of the grid, the cell).
Get row index like this:
GridViewRow gvr = (GridViewRow)(((HyperLink)e.CommandSource).NamingContainer);
int index= gvr.RowIndex;
// to get image url
string url = ((Image)gvr.FindControl("Image3")).ImageUrl;
Image3.ImageUrl= url;

How to hide a column inside a repeater when checkbox is checked?

What I am trying to do is access a column (which has the header title "Add to Shopping List") inside a repeater (areaRepeater) that is inside another repeater (locationRepeater). I want to hide it if a checkbox is checked. However, despite how things are setup, the column is never hidden. I'm not sure what I'm missing here...or maybe I'm going about it the wrong way?
Here is the piece of code that I am trying to use to hide the column in the areaRepeater table. I can hide the submitBtn button successfully, but that button is not inside a repeater.
Sitecore.Data.Fields.CheckboxField checkBox = ProductGroup.Fields["Shopping Disabled"];
if (checkBox.Checked)
{
submitBtn.Visible = false;
Repeater rpt1 = (Repeater)FindControl("locationRepeater");
Response.Write("a ");
foreach (RepeaterItem rep in rpt1.Items)
{
Response.Write("1 ");
Repeater areaRepeater = (Repeater)rep.FindControl("areaRepeater");
foreach (RepeaterItem areaRep in areaRepeater.Items)
{
Response.Write("2 ");
if (showField() == false)
{
Label lbl1 = (Label)areaRep.FindControl("litCol");
CheckBox check = (CheckBox)areaRep.FindControl("LineQuantity");
lbl1.Visible = false;
check.Visible = false;
}
}
}
}
This is the designer code for both repeaters. I tried setting the visibility of the label for the header and the checkbox with a function called showField() but it is never called, even though it does return the correct bool value:
<asp:Repeater ID="locationRepeater" runat="server" OnItemDataBound="SetInner">
<ItemTemplate>
<div class="LocationName">
<%# Eval("SecOpen") %><%# Eval("LocationName")%> <%# Eval("SecClose") %>
</div>
<asp:Repeater ID="areaRepeater" runat="server">
<HeaderTemplate>
<div class="headerRow">
<div class="header">
<div class="thumb"><p></p></div>
<div class="headerField name"><p class="hField">Product</p></div>
<div class="headerField sku"><p class="hField">SKU</p></div>
<div class="headerField size"><p class="hField">Size</p></div>
<div class="headerField case"><p class="hField">Case Pack</p></div>
<div class="headerField use"><p class="hField">Use With</p></div>
<div id="shoppingHeader" class="headerField qty" runat="server"><p class="headerfield qty hField"><asp:Label id="listCol" runat="server" visible='<%# showField() %>' Text="Add To Shopping List" /> </p></div>
</div>
</div>
</HeaderTemplate>
<ItemTemplate>
<asp:placeholder id="LocationAreaHeader" runat="server" visible='<%# (Eval("AreaName").ToString().Length == 0 ? false : true) %>' ><h3> <%# Eval("AreaName") %></h3></asp:placeholder>
<asp:placeholder id="ProductTable" runat="server" visible='<%# (Eval("ProductName").ToString().Length == 0 ? false : true) %>' >
<div class="table">
<div class="row">
<div class="thumb"><%# Eval("Charm") %></div>
<div class="field name"><p class="pField"> <%# Eval("ThumbOpen") %><%# Eval("ProductName") %><%# Eval("ThumbClose") %></p> </div>
<div class="field sku"><p class="pField"> <%# Eval("Sku") %> </p></div>
<div class="field size"><p class="pField"> <%# Eval("Size") %></p></div>
<div class="field case"><p class="pField"> <%# Eval("CasePack") %> </p></div>
<div class="field use"><p class="pField"> <%# Eval("UseWith") %> </p></div>
<div id="shopping" class="field qty" runat="server"><p class="pField"> <asp:checkbox visible='<%# showField() %>' id="LineQuantity" runat="server" /></p></div>
</div>
</div>
<asp:Label id="productID" text='<%# Eval("productID") %>' visible="false" runat="server" />
</asp:placeholder>
<!-- Stored values -->
<asp:Label id="SkuID" runat="server" text='<%# Eval("SkuID") %>' visible="true" />
<asp:Label id="masterSku" runat="server" text='<%# Eval("masterSku") %>' visible="false" />
<asp:Label id="masterName" runat="server" text='<%# Eval("masterName" ) %>' visible="false" />
<asp:Label ID="test" visible="false" runat="server" text='<%# Eval("AreaID") %>' />
</ItemTemplate>
</asp:Repeater>
<asp:Label ID="refID" visible="false" runat="server" text='<%# Eval("LocationID") %>' />
</ItemTemplate>
</asp:Repeater>
This is the showField() function:
protected bool showField()
{
bool retVal = true;
Item CurrentItem = Sitecore.Context.Item;
Item HomeItem = ScHelper.FindAncestor(CurrentItem, "Market");
if (HomeItem != null)
{
Item ProductGroup = HomeItem.Axes.SelectSingleItem(#"child::*[##templatename='MarketOfficeBuildigProductMap']");
if (ProductGroup != null)
{
Sitecore.Data.Fields.CheckboxField checkBox = ProductGroup.Fields["Shopping Disabled"];//curently returns true
ShoppingDisabled.Value = checkBox.Checked.ToString();
if (checkBox.Checked == true)
{
retVal = false;
}
}
}
return retVal;
}
I think you could approach this from a different angle which could make your code a lot simpler.
First off try not to use the OnDataBound event. I prefer to use the DataBinding event for the specific controls as it localizes the code better and you never have search for controls. I am not exactly sure how your code is working but take a look at how I would implement the two controls you are trying to hide to use the DataBinding event.
e.g. for the Checkbox:
<asp:CheckBox id="LineQuantity" runat="server" OnDataBinding="LineQuantity_DataBinding" />
protected void LineQuantity_DataBinding(object sender, System.EventArgs e)
{
CheckBox chk = (CheckBox)sender;
chk.Checked = (bool)(Eval("SomeField")); // Note you can use Eval here...
chk.Visible = showField();
}
This should help you localize the problem at least. I typically use this method of doing any custom work while databinding. The DataBound event is not great because it happens after it is all done. The OnDataBinding event lets you inject whatever type of logic you want at the time the databinding is occurring.

Transferring DataBound ID from aspx to codebehind

I have an Unsubscribe page(Unsubscribe.aspx) that generates three radio buttons and a text field by using a repeater.
The id's of the radio buttons come from databinding and I need to get those ID's to use in the code page but I can't seem to get it to recognize the existence of this binding.
Unsubscribe.aspx looks like this:
<asp:Repeater ID="rptReasons" runat="server">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<div>
<div class="col-lg-12">
<label class="radio-custom">
<input id='rbReason<%# DataBinder.Eval(Container, "DataItem.ID")%>' type="radio" name="rbReason" value='<%# DataBinder.Eval(Container, "DataItem.ID")%>'>
<i class="fa fa-circle-o"></i>
<label for='rbReason<%# DataBinder.Eval(Container, "DataItem.ID")%>'><%# DataBinder.Eval(Container, "DataItem.Explanation")%></label>
</label>
<%# (DataBinder.Eval(Container.DataItem, "ReasonType").ToString() == "OTH") ? "<input class='form-control m-t m-l-n-md' name='txtComment' type='text' maxlength='100' style='width:300px;' />" : "" %>
</div>
</div>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
And this is the behaviour I want to achieve:
if (DataItem.ID == checked )
{
disable the text area
}
try to add
runat="server"
<input runat="server" id='rbReason<%# DataBinder.Eval(Container, "DataItem.ID")%>' type="radio" name="rbReason" value='<%# DataBinder.Eval(Container, "DataItem.ID")%>'>
after that you need to find control on event
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
{
Label lbl = (Label)e.Item.FindControl("Label1");
LinkButton link = (LinkButton)e.Item.FindControl("LinkButton1");
RadioButton rdbtn = (RadioButton)e.Item.FindControl("control_id");
}
}

show div dynamically when user entered number in a textbox

I have textbox with an id #txtUserEntry where user can put the number of their family member living in their house and a button #btnAddOccupant to click after they put a number in textbox and #divOccupantProfile should show up depends on the number entered so it means 1 family member is equal to 1 #divOccupantProfile
aspx code
<tr>
<td style="text-align:left"><asp:Label ID="lbUserEntry" runat="server" Text="Number of House occupant"></asp:Label></td>
<td><asp:TextBox ID="txtUserEntry" class="basetxt" runat="server" Width="290"></asp:TextBox></td>
</tr>
<tr>
<td style="text-align:left"><asp:Button ID="btnAddOccupant" runat="server" Text="+" />
<asp:Label ID="lbres5" runat="server" Text="Add Occupant"></asp:Label></td>
</tr>
divOccupantProfile
<div id="divOccupantProfile">
<asp:Label ID="OPfamilyname" runat="server" Text="Family Name"></asp:Label>
<asp:TextBox ID="textOPfamilyname" runat="server"></asp:TextBox><br />
<asp:Label ID="OPfirstname" runat="server" Text="First Name"></asp:Label>
<asp:TextBox ID="textOPfirstname" runat="server"></asp:TextBox><br />
<asp:Label ID="OPmiddlename" runat="server" Text="Middle Name"></asp:Label>
<asp:TextBox ID="textOPmiddlename" runat="server"></asp:TextBox><br />
<asp:Label ID="OPmaritalstatus" runat="server" Text="Marital Status"></asp:Label>
<asp:DropDownList ID="ddlOPmaritalstatus" runat="server" >
<asp:ListItem></asp:ListItem>
<asp:ListItem>Married</asp:ListItem>
<asp:ListItem>Single</asp:ListItem>
<asp:ListItem>Divorced</asp:ListItem>
</asp:DropDownList><br />
<asp:Label ID="OPoccupation" runat="server" Text="Occupation"></asp:Label>
<asp:TextBox ID="textOPoccupation" runat="server"></asp:TextBox><br />
<asp:Label ID="OPrelationship" runat="server" Text="Relationship"></asp:Label>
<asp:DropDownList ID="ddlOPrelationship" runat="server" >
<asp:ListItem></asp:ListItem>
<asp:ListItem>Wife</asp:ListItem>
<asp:ListItem>Daughter</asp:ListItem>
<asp:ListItem>Son</asp:ListItem>
<asp:ListItem>Father</asp:ListItem>
<asp:ListItem>Mother</asp:ListItem>
<asp:ListItem>House helper</asp:ListItem>
<asp:ListItem>Driver</asp:ListItem>
</asp:DropDownList>
</div>
can someone show me an example how to do this one
something like
$('#btnAddOccupant').click(function(){
var occupants = $('#txtUserEntry').val();
//here some check on occupants.....
for(int i = 0;i<occupants;i++){
$(somewhere).append($('#divOccupantProfile').html());
}
})
Some suggestions:
because you are going to use multiple divOccupantProfile, it's better to use selectors based on the class instead of ID, otherwise in your page you'll have 1+ elements with the same ID.
The $(somewhere)
is the "container" where you want to put all the div related to each occupant profile
Also note that the .html() function will copy the html INSIDE the div, exluding the div itself. So if you want to have N elements formed in this way
<div class="occupantProfile">...</div>
<div class="occupantProfile">...</div>
you have to use
somewhere.append($('<div').append($('.occupantProfile')).html())
Here i have made a sample for what you should find helpful
HTML
<input type="text"http://jsfiddle.net/#save id="txtnum" />
<input type="button" value="click" id="btnSubmit" />
<div id="divOccupantProfile1">Data 1</div>
<div id="divOccupantProfile2">Data 2</div>
<div id="divOccupantProfile3">Data 3</div>
<div id="divOccupantProfile4">Data 4</div>
<div id="divOccupantProfile5">Data 5</div>
CSS
div[id^="divOccupantProfile"] {
display:none;
}
Jquery
$("#btnSubmit").click(function(){
var num = $("#txtnum").val();
$("#divOccupantProfile"+ num).css("display","block");
})
Demo Link

Setting checkbox to be checked/unchecked depending on the column result in datalist

I have got 2 check box[isSold(bit), Offline(bit)] in the datalist , I want to set that check box to be checked or unckecked depending on the database result of that row. For instance if the product is sold , the sold column will be true , therefore the checkbox should be checked. I tried to do acheive the required result by using code below with no success:
<asp:DataList ID="dlEditCaravans" runat="server"
RepeatDirection="Vertical" DataKeyField="makeID" OnEditCommand="edit"
OnDeleteCommand="delete" ItemStyle-CssClass="dleditCaravans"
onitemcommand="dlEditCaravans_ItemCommand"
onitemdatabound="dlEditCaravans_ItemDataBound">
<ItemTemplate>
<div class="imgeditCaravan">
<asp:Image runat="server" ID="caravanImage" ImageUrl='<%#String.Format("/images/caravans/{0}", Eval("image")) %>' Height="80" Width="80" />
</div>
<div class="lblmakeCaravan">
<asp:Label ID="lblMake" runat="server" Text='<%#Eval("make") %>' CssClass="lblheader"></asp:Label>
<br />
<asp:Label ID="imgDesc" runat="server" CssClass="lblDescription" Text='<%#(Eval("description").ToString().Length>350)?Eval("description").ToString().Substring(0,50)+ "....":Eval("description").ToString() + "...." %>'></asp:Label>
<br />
<asp:Label ID="lblPrice" runat="server" Text='<%#"£ "+Eval("Price")%>'></asp:Label>
<br />
</div>
<div class="editImage">
<asp:ImageButton ID="edit" runat="server" CommandName="edit" ImageUrl="~/images/newsControls/edit.gif" ToolTip="Edit Caravan"/>
<asp:ImageButton ID="delete" runat="server" CommandName="delete" ImageUrl="~/images/newsControls/delete.gif" ToolTip="Delete Caravan"/>
<br /> <br />
<asp:Button ID="btnAddToFeature" runat="server" Text="Add To Feautured Caravans" CommandName="AddToCaravans" Enabled="false" Width="210" Height="30" ForeColor="#1D91BD" ToolTip="Add Caravan To Feautured Caravans"/><br /><br />
<asp:Button ID="btnRemoveFeature" runat="server" Text="Remove From Feautured Caravans" CommandName="RemoveToCaravans" Enabled="false" ToolTip="Remove from Featured Caravans" Width="210" Height="30" ForeColor="#1D91BD" />
<br /> <br />
<asp:CheckBox runat="server" ID="chkOffline" Checked='<%#Eval("offline") %>' /> <label>Set This Caravan in Offline mode</label>
</div>
</ItemTemplate>
<SeparatorTemplate>
<div class="descSeparator"></div>
</SeparatorTemplate>
</asp:DataList>
First of all, this should work: Checked='<%#Eval("offline") %>', if it does not work, it must be a different problem.
Alternatively you can do the same in the ItemDataBound event. like..
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
{
DataRow dr = ((DataRowView)e.Item.DataItem).Row;
((CheckBox)e.Item.FindControl("chkOffline")).Checked = Convert.ToBoolean(dr["chkOffline"]);
}
}
}
What you can do is, store the value in instead of the checkbox and then on the ItemDatabound event you can add find the value of the checkbox from the label and assign the value of checked or not to checkbox.

Categories