Transferring DataBound ID from aspx to codebehind - c#

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");
}
}

Related

How do you make labels visible again once their visibility is set to false?

In asp.net webforms I have a UserControl with a code-behind file (shown below).
The UserControl has two <asp:Label> nodes in it that I am attempting to use as steps in a form.
The problem is that when I get to "Step2" and click Cancel, "Step1" does not re-display, despite the fact that I am setting its Visible property to true.
What am I doing wrong? Or is there a better way to do this?
UserControl
<%# Control Language="C#" AutoEventWireup="true"
CodeFile="MyUserControl.ascx.cs" Inherits="_MyUserControl" %>
<asp:Label runat="server" ID="lblStep1" Visible="true">
<fieldset>
<p>Some initial text here</p>
<asp:Button runat="server" CssClass="btn btn-primary" ID="cmdSubmit" Text="Submit" />
</fieldset>
</asp:Label>
<asp:Label runat="server" ID="lblStep2" Visible="false">
<p>Some text here</p>
<div>
<asp:Button runat="server" CssClass="btn btn-primary" ID="cmdRequest" Text="Send The Request" />
<asp:Button runat="server" CssClass="btn" ID="cmdCancel" Text="Cancel" />
</div>
</asp:Label>
Code-behind
public partial class _MyUserControl : UserControl
{
protected void Page_Init(object sender, EventArgs e)
{
cmdCancel.Click += new EventHandler(cmdCancel_Click);
cmdSubmit.Click += new EventHandler(cmdSubmit_Click);
}
public void cmdSubmit_Click(object sender, EventArgs e)
{
lblStep1.Visible = false;
lblStep2.Visible = true;
}
public void cmdCancel_Click(object sender, EventArgs e)
{
// return to the previous step
lblStep1.Visible = true;
lblStep2.Visible = false;
}
}
you are not doing anything with those labels and that's a wrong idea to have buttons defined inside label control. Looks like you are trying to use label as container control. Rather use a <div> element/tag as container and try. It should work.
<div runat="server" ID="lblStep1" Visible="true">
<fieldset>
<p>Some initial text here</p>
<asp:Button runat="server" CssClass="btn btn-primary" ID="cmdSubmit" Text="Submit" />
</fieldset>
</div>
<div runat="server" ID="lblStep2" Visible="false">
<p>Some text here</p>
<div>
<asp:Button runat="server" CssClass="btn btn-primary" ID="cmdRequest" Text="Send The Request" />
<asp:Button runat="server" CssClass="btn" ID="cmdCancel" Text="Cancel" />
</div>
</div>

ASP repeater collecting data from radiobuttons

So I have a Asp repeater that prints out some radiobuttons that a user will use to answer a test. What I need to do is to check which radiobuttons are checked so that I can save them to a XML file.
Here is the code
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<div class="repeaterDiv">
<ul id="list">
<li style="list-style: none">
<p style="font-weight: bold">Fråga nummer: <%# Container.ItemIndex + 1 %></p>
<asp:Label ID="q1_label" runat="server" Text='<%#Eval ("text")%>'></asp:Label>
<asp:CheckBox ID="q1_svar1" Text='<%#Eval("q1")%>' runat="server" CssClass="radioButtons;" />
<asp:CheckBox ID="q1_svar2" Text='<%#Eval("q2")%>' runat="server" CssClass="radioButtons;"/>
<asp:CheckBox ID="q1_svar3" Text='<%#Eval("q3") %>' runat="server" CssClass="radioButtons;"/>
<asp:CheckBox ID="q1_svar4" Text='<%#Eval("q4") %>' runat="server" CssClass="radioButtons;"/>
<hr />
</li>
</ul>
</div>
</ItemTemplate>
</asp:Repeater>
I dont really know how to target the checked ones, what do I do from here? Thanks in advance.
You can use foreach loop in repeater items and them user FindControl method
protected void Button1_Click(object sender, EventArgs e)
{
foreach (RepeaterItem item in Repeater1.Items)
{
CheckBox chk = (CheckBox)item.FindControl("q1_svar1");
if (chk.Checked)
{
//Your code ...
}
}
}

ASP.NET button CommandArgument using <% %>

I have the following snippet of code in my .aspx file, I want the button to send the ID to the code behind.
<%List<Notes> CommentsList = FindNotes(ParentNote.ID); %>
<%foreach (var comment in CommentsList) { %>
<div id="note" class="tierTwo"><p><%: comment.Content %></p><ul><li><%: comment.AddedDate %></li><li>20:29</li><li><%: comment.AddedByName %></li><li>Add comment</li></ul> >
<div id="addComment<%:comment.NoteID%>" class="tierOne" style="display:none">
<asp:TextBox ID="commentreplytext" runat="server" rows="4"> </asp:TextBox>
<asp:HiddenField id="ParentIdReply" value='<%=comment.NoteID%>' runat="server" />
<asp:Button ID="Button2" runat="server" Text="Add" CommandArgument="<%: comment.NoteID%>" OnCommand="Add_Comment" />
</div>
</div>
The code behind currently looks like this
protected void Add_Comment(object sender, CommandEventArgs e)
{
string uniqueNoteID = e.CommandArgument.ToString();
}
The command argument is getting sent to the Add_Comment method but it returns "<%: comment.NoteID%>" rather than the value it represents. This way of retrieving the NoteID value works fine when setting the div id so I don't know why it is causing an issue in the commandArgument.
Unfortunately you can't use <%%> code blocks in .NET controls. It's to do with the order things get rendered in. You'll have to set the CommandArgument in the code behind when you build the page. You could do that if you built the page using a repeater rather than a for each in the page.
<asp:Repeater runat="server" id="repeater1">
<ItemTemplate>
<div id="note" class="tierTwo"><p><%# Eval("Content") %></p><ul>
<li><%# Eval("AddedDate") %></li><li>20:29</li>
<li><%# Eval(AddedByName") %></li><li>
Add comment</li></ul>
<div id='addComment<%# Eval("NoteID") %>' class="tierOne" style="display:none">
<asp:TextBox ID="commentreplytext" runat="server" rows="4"> </asp:TextBox>
<asp:HiddenField id="ParentIdReply" value='<%#Eval("NoteID")%>' runat="server" />
<asp:Button ID="Button2" runat="server" Text="Add" CommandArgument='<%#Eval("NoteID")%>' OnCommand="Add_Comment" />
</div>
</div>
</ItemTemplate>
</asp:Repeater>
.cs page:
repeater1.DataSource = FindNotes(ParentNote.ID);
repeater1.DataBind();

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.

how to find checked radio button in a gridview?

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

Categories