ASP.NET CheckBox Control showing as a Literial - c#

I have a nested repeater that contains a CheckBox control.
<asp:Repeater runat="server" ID="optionalRepeater" OnItemDataBound="CheckCompCondiments">
<HeaderTemplate>
<div class="sectionHeader" style="font-weight: bold; font-size: 13pt; text-decoration: underline; margin: 20px 0px 15px 0px;">
Product Extras
</div>
</HeaderTemplate>
<ItemTemplate>
<div class="col-md-4" style="border: 1px solid #ccc; margin: 5px;">
<asp:HiddenField runat="server" ID="salesMenuID" Value='<%#Eval("sales_menu_id") %>' />
<asp:HiddenField runat="server" ID="minVal" Value='<%#Eval("minVal") %>' />
<asp:HiddenField runat="server" ID="maxVal" Value='<%#Eval("maxVal") %>' />
<p>
<asp:Label runat="server" ID="condHdr" style="text-decoration: underline;" Text='<%#Eval("sales_menu_desc") %>'></asp:Label>
</p>
<asp:Repeater runat="server" ID="compMenuRadio">
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<asp:HiddenField runat="server" ID="hdPriLvl"/>
<input type="radio" name="compSelect" runat="server"/>
</ItemTemplate>
<FooterTemplate>
<hr/>
</FooterTemplate>
</asp:Repeater>
<asp:Repeater runat="server" ID="compMenuCheck">
<HeaderTemplate></HeaderTemplate>
<ItemTemplate>
<asp:HiddenField runat="server" ID="hdPriLvl" Value='<%#Eval("pPriceLvl") %>' />
<asp:CheckBox runat="server" ID='compSelected' CssClass='<%# Eval("pcode") %>' Text='<%# Eval("pdesc") %> '/>
<%--<input type="checkbox" id="compSelect" runat="server" value='<%#Eval("pCode") %>'/>--%>
<%-- <%#Eval("pDesc") %>--%>
</ItemTemplate>
<FooterTemplate>
</FooterTemplate>
</asp:Repeater>
<asp:DropDownList runat="server" ID="compSelect" Visible="False" style="width: 98%; margin: 5px 0px 5px 0px;"/>
</div>
</ItemTemplate>
</asp:Repeater>
On my Server Side I have the following code:
if (optionalRepeater.Items.Count > 0)
{
foreach (RepeaterItem compItem in optionalRepeater.Items)
{
HtmlInputHidden priLvlVal = (HtmlInputHidden)compItem.FindControl("hdPriLvl");
foreach (Control compCtrl in compItem.Controls)
{
if (compCtrl.GetType() == typeof(CheckBox))
{
CheckBox checkBox = (CheckBox)compCtrl;
CheckBox chk = (CheckBox) checkBox;
if (checkBox.Checked)
{
string prod = checkBox.CssClass;
string childGuiud = Guid.NewGuid().ToString("N").ToUpper();
hasCondiment = true;
ShoppingCart.Instance.AddItem(prod, childGuiud, mainGuid, Session["transactionGuid"].ToString(), priLvlVal.Value, hasCondiment, 1, null);
checkBox.Checked = false;
}
}
}
}
}
My code get as far as this line
if (compCtrl.GetType() == typeof(CheckBox))
But never makes it inside the if statement. When I am expecting the type to be CheckBox it says literal Control.
Can anyone see anything that I could be doing wrong, I have been looking at this for a while now and I can't seem to get any further.
Thanks,
Somango!

If you use an ItemTemplate you have to use FindControl to get the reference of the controls inside:
foreach (RepeaterItem compItem in optionalRepeater.Items)
{
HtmlInputHidden priLvlVal = (HtmlInputHidden)compItem.FindControl("hdPriLvl");
CheckBox compSelect = (CheckBox)compItem.FindControl("compSelect");
if (checkBox.Checked)
{
// ...
}
}
By looping the RepeaterItem.Controls property you just find a single Literal control which is the container for every control in the ItemTemplate. So you could loop compItem.Controls[0].Controls to find your CheckBox. But in my opinion using compItem.FindControl is much more readable.

Related

Get modal popup values in gridview row textbox

I have a gridview with dynamic controls. Resource name is something that I am picking up from Active directory. Search filter works in a way where you have to enter lastname, firstname and onlclick of lookup it will search. If there are more than one record a modalpopup will be displayed with dropdownlist with options to select from.
The issue is that I am unable to get the dropdown values populated into textbox present in gridview. I need this dropdown value in correct selected row's textbox.
Code behind:
protected void gvProjectResource_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Lookup")
{
GridViewRow clickedRow = ((LinkButton)e.CommandSource).NamingContainer as GridViewRow;
TextBox t2 = ((TextBox)clickedRow.FindControl("t2"));
DataTable dt = globalObj.FindPersons(t2.Text.Split(',')[0].Trim(), t2.Text.Split(',')[1].Trim());
if (dt.Rows.Count > 1)
{
mpFindPerson.Show();
ddlPersons.Items.Clear();
ddlPersons.Items.Add(new ListItem { Value = "0", Text = "-Select User-" });
ddlPersons.DataSource = dt;
dt.Columns.Add("FullName", typeof(string), "DisplayName + ' | ' + MSID");
ddlPersons.DataTextField = "FullName";
ddlPersons.DataValueField = "MSID";
ddlPersons.DataBind();
}
else
{
t2.Text = dt.Rows[0].ItemArray[0].ToString();
}
}
if (e.CommandName == "Del")
{
}
}
protected void btnSelectPerson_Click(object sender, EventArgs e)
{
TextBox t2 = (gvProjectResource.SelectedRow.FindControl("t2") as TextBox);
t2.Text = ddlPersons.SelectedItem.Text;
mpFindPerson.Hide();
}
ASPX Code:
<div id="four">
<fieldset style="border: solid; border-width: thin; height: 220px; border-color: #a8a8a8;">
<legend id="Legend11" runat="server" visible="true" style="width: auto; margin-bottom: 0px; font-size: 12px; font-weight: bold; color: #1f497d;"> Project Resources </legend>
<div style="height: 210px; overflow: auto;">
<asp:GridView ID="gvProjectResource" Width="88%" HeaderStyle-Font-Size="X-Small" CssClass="labels" runat="server"
ShowFooter="true" AutoGenerateColumns="false" Style="margin-right: auto; margin-left:7px;"
OnRowDataBound="gvProjectResource_RowDataBound"
OnRowCommand="gvProjectResource_RowCommand" >
<Columns>
<asp:TemplateField HeaderText="Role" ItemStyle-VerticalAlign="Top" FooterStyle-VerticalAlign="Top" ItemStyle-Width="50%">
<ItemTemplate>
<asp:Label ID="lblRole" runat="server" Text='<%# Eval("role") %>' Visible="false" />
<asp:DropDownList ID="ddlRole" Height="23px" Width="98%" runat="server" CssClass="DropDownListStyleOverview"></asp:DropDownList>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlRole2" Height="23px" Width="98%" runat="server" CssClass="DropDownListStyleOverview"></asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Resource Name" ItemStyle-VerticalAlign="Top" FooterStyle-VerticalAlign="Top" ItemStyle-Width="50%">
<ItemTemplate>
<asp:TextBox ID="t1" Height="23px" Width="98%" runat="server" ></asp:TextBox>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="t2" Height="23px" Width="68%" runat="server"></asp:TextBox>
<asp:LinkButton runat="server" Text="Lookup" CommandName="Lookup" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-VerticalAlign="Top" ItemStyle-Width="5%">
<ItemTemplate>
<asp:LinkButton runat="server" Text="Delete" CommandName="Del" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<div style="text-align: right;">
<asp:LinkButton runat="server" Text="Add New" ID="LinkButton2" CssClass="labels"></asp:LinkButton>
</div>
</div>
<asp:Button runat="server" ID="btnModalPopUpSearch" Style="display: none" />
<AjaxToolkit:ModalPopupExtender ID="mpFindPerson"
BehaviorID="mpFindPersonBehav"
runat="server"
DropShadow="true"
BackgroundCssClass="modalBackground"
PopupControlID="pnlFindPerson"
TargetControlID="btnModalPopUpSearch">
</AjaxToolkit:ModalPopupExtender>
<asp:Panel runat="Server" ID="pnlFindPerson" CssClass="modalPopup" style="position:relative;">
<div class="labels" runat="server" style="text-align:center; position:absolute; top:20%; height:10em; margin-top:auto; margin-left:20px;">
<asp:DropDownList ID="ddlPersons" runat="server" AppendDataBoundItems="true" Width="200px">
<asp:ListItem Text="-Select User-" Value="0" />
</asp:DropDownList>
<br />
<br />
<br />
<div style="text-align: center;">
<asp:Button runat="server" Font-Size="9pt" Text="Select" CssClass="buttons___" Style="float: left;"
ID="btnSelectPerson" OnClick="btnSelectPerson_Click" />
</div>
</div>
</asp:Panel>
</fieldset>
</div>
ok, so, there was nothing much to do.
have to add this.
protected void btnSelectPerson_Click(object sender, EventArgs e)
{
TextBox t2 = (TextBox)gvProjectResource.FooterRow.FindControl("t2");
t2.Text = ddlPersons.SelectedItem.Text;
mpFindPerson.Hide();
}

Button action event to get row details of grid view inside repeater

I am having some problem when trying to get the rows with marked checkbox in grid view. My grid view was set up using a repeater, here is the code:
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
<ItemTemplate>
<!-- COLLAPSIBLE PANEL EXTENDER -->
<asp:Panel ID="pHeader1" runat="server" CssClass="cpHeader">
<!-- Collapsible panel extender header -->
<div class="form-group" style="background-color: #ffb848; height: 30px; vertical-align: middle">
<div class="col-md-6">
<div style="float: left; color: White; padding: 5px 5px 0 0">
<asp:Label ID="lblProduct" Text='<%# DataBinder.Eval(Container.DataItem, "name") %>' runat="server" />
</div>
</div>
<div class="col-md-6">
<div style="float: right; color: White; padding: 5px 5px 0 0">
<asp:Label ID="lblHeaderText1" runat="server" />
</div>
</div>
<div style="clear: both"></div>
</div>
</asp:Panel>
<!-- Collapsible panel extender body -->
<asp:Panel ID="pBody1" runat="server" CssClass="cpBody">
<asp:Label ID="lblBodyText1" runat="server" />
<!-- Grid view to show products based on each category -->
<asp:GridView ID="gvProduct" runat="server" AutoGenerateColumns="False" Width="725px" CellPadding="4" ForeColor="#333333" GridLines="None" ShowHeader="False" DataKeyNames="id">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:CheckBox ID="cbCheckRow" runat="server" ItemStyle-Width="50px"/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="id" HeaderText="Name" ItemStyle-Width="50px" />
<asp:BoundField DataField="name" HeaderText="Name" ItemStyle-Width="450px" />
<asp:BoundField DataField="unitQuantity" HeaderText="Unit Quantity" />
<asp:TemplateField HeaderText="Quantity" ItemStyle-HorizontalAlign="Center" ItemStyle-Width="200px">
<ItemTemplate>
<asp:TextBox ID="tbQuantity" runat="server" Width="40" Text="0" OnTextChanged="tbQuantity_TextChanged" AutoPostBack="true"/>
<asp:Label ID="lblCheckAmount" runat="server" ForeColor="#a94442"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</asp:Panel>
<asp:CollapsiblePanelExtender ID="cpe1" runat="server" TargetControlID="pBody1" CollapseControlID="pHeader1"
ExpandControlID="pHeader1" Collapsed="true" TextLabelID="lblHeaderText1" CollapsedText="Show"
ExpandedText="Hide" CollapsedSize="0"
ScrollContents="false">
</asp:CollapsiblePanelExtender>
</ItemTemplate>
</asp:Repeater>
What I am trying to do is, when a button is on click, it will get the datakey which is id for the rows with marked checkbox and store them into a list. So how am I supposed to write the code to loop each row of grid view to check if the check box has marked and store them into a string list?
Thanks in advance.
Let's say you want to put the DataKey values in a List<string>, then use a nested loop inside the Repeater and the GridView. The below code will collect all of the values in idList:
List<string> idList = new List<string>();
foreach (RepeaterItem ri in Repeater1.Items)
{
GridView gvProduct = (GridView)ri.FindControl("gvProduct");
foreach (GridViewRow gr in gvProduct.Rows)
{
CheckBox cb = (CheckBox)gr.FindControl("cbCheckRow");
if (cb.Checked)
{
// add the corresponding DataKey to idList
idList.Add(gvProduct.DataKeys[gr.RowIndex].Value.ToString());
}
}
}
Try this code ,
foreach (RepeaterItem i in Repeater1.Items)
{
GridView _gv = (GridView)i.FindControl("gvProduct");
foreach (GridViewRow _g in _gv.Rows)
{
CheckBox chk = (CheckBox)_g.FindControl("cbCheckRow") ;
if(chk.Checked)
{
.......
}
}
}

how to find linkbutton value on clicking checkbox in datalist using c#?

this is what i have done:
<ajaxToolkit:TabContainer ID="TabContainer1" runat="server" ActiveTabIndex="0">
<ajaxToolkit:TabPanel ID="TabPanel1" runat="server" HeaderText="TabPanel1">
<HeaderTemplate>
Pro
</HeaderTemplate>
<ContentTemplate>
<div style="height: 100px; width: 200px; overflow: Auto">
<asp:DataList ID="DataList1" runat="server" CellPadding="2" CellSpacing="2" ForeColor="Red">
<ItemTemplate>
<asp:CheckBox ID="checkbox" runat="server" AutoPostBack="true" OnCheckedChanged="checkLike_CheckedChanged" />
<asp:LinkButton Text='<%#Eval("UPP")%>' ID="lnkpro" runat="server" CssClass="linkbutton"
OnClick="btn_Click" CommandArgument='<%# Eval("UCode") %>'></asp:LinkButton>
</ItemTemplate>
</asp:DataList>
</div>
</ContentTemplate>
</ajaxToolkit:TabPanel>
<ajaxToolkit:TabPanel ID="TabPanel2" runat="server" HeaderText="services">
<HeaderTemplate>
Sets
</HeaderTemplate>
<ContentTemplate>
<div style="height: 100px; width: 200px; overflow: Auto">
<asp:DataList ID="DataList2" runat="server" CellPadding="2" CellSpacing="2">
<ItemTemplate>
<asp:CheckBox ID="checkbox" runat="server" />
<asp:LinkButton Text='<%#Eval("SNA")%>' ID="lnkpro1" runat="server" CssClass="linkbutton"
OnClick="btn_Click1" CommandArgument='<%# Eval("Code") %>'></asp:LinkButton>
</ItemTemplate>
</asp:DataList>
</div>
</ContentTemplate>
</ajaxToolkit:TabPanel>
</ajaxToolkit:TabContainer>
Now my question :
This code block results in a list of link buttons in front of them all checkboxes. So what i want is to get the respective linkbutton value on checkbox checkchanged.
Is it possible?
for example
[] linkbtn1
[] linkbtn2
when i check on first checkbox the id not text of linkbutton1 should be fetched.([] is checkbox )
The ID of each link button is going to be the same on each row of the data list, the text value of each link button will be the SNA value bound to that link button. You can retrieve that SNA value by using the check box's CheckChanged event, like this:
In the DataList's ItemTemplate markup:
<asp:CheckBox ID="checkbox" runat="server"
OnCheckedChanged="checkbox_CheckChanged" />
Now that we have wired up the check changed event, it is time to implement the handler, like this:
protected void checkbox_CheckChanged(object sender, EventArgs e)
{
CheckBox theCheckBox = sender as CheckBox;
// Make sure the cast to check box worked before we try to use it
if(theCheckBox != null)
{
LinkButton theLinkButton = theCheckBox.Parent.FindControl("lnkpro1") as LinkButton;
// Verify that the link button was found before we try to use it
if(theLinkButton != null)
{
// Get the text value from the link button in the same row
// as the check box checked changed
string theLinkButtonText = theLinkButton.Text;
// Do something with text value here
}
}
}
Try this
protected void checkbox_CheckedChanged(object sender, EventArgs e)
{
foreach (DataListItem item in DataList1.Items)
{
CheckBox chk = (CheckBox)item.FindControl("checkbox");
LinkButton lnkbtnActivate = (LinkButton)item.FindControl("lnkpro");
if (chk.Checked == true)
{
string Result = lnkbtnActivate.Text;
}
}
}

ASP.NET Maintain scroll position after postback in div inside data list and user control

My situation looks like this: I've got x-axis scrollable div that contains buttons with numbers of page displayed. This div is placed in data list inside User Control responsible for displaying news on my site.
This is the code of this div and datalist with page numbers.
<div style="width:430px; overflow:auto; overflow-y:hidden; -ms-overflow-y:hidden; vertical-align:top; position:relative; top:-1px; ">
<asp:DataList ID="dlPaging" runat="server" OnItemCommand="dlPaging_ItemCommand" RepeatDirection="Horizontal"
OnItemDataBound="dlPaging_ItemDataBound">
<ItemTemplate>
<asp:Button ID="lnkbtnPaging" class="pagebutton" runat="server" CommandArgument='<%# Eval("PageIndex") %>'
CommandName="lnkbtnPaging" Text='<%# Eval("PageText") %>' CausesValidation="False" />
</ItemTemplate>
</asp:DataList>
</div>
How to mantain x-axis position of this div after postback? I've tried several tricks, javascript and I can't figure it out.
Any reason not to use an UpdatePanel?
<div id="divDtPaging" runat="server" visible="true" style="width: 50%; overflow: scroll; text-align: center">
<asp:DataList runat="server" ID="dtPaging" OnItemCommand="dtPaging_ItemCommand"
OnItemDataBound="dtPaging_ItemDataBound" RepeatDirection="Horizontal"
SeparatorStyle-Wrap="true" Style="height: auto">
<ItemTemplate>
<asp:LinkButton runat="server" ID="lnkbtnPaging" Text="PageText" CommandArgument="PageIndex" Style="padding-right: 5px">
</asp:LinkButton>
</ItemTemplate>
</asp:DataList>
</div>
<asp:HiddenField id="hdnScrollPos" runat="server"/>
<script type="text/javascript">
function BeginRequestHandler(sender, args)
{
document.getElementById('<%=hdnScrollPos.ClientID %>').value = document.getElementById('<%=divDtPaging.ClientID %>').scrollLeft;
}
function EndRequestHandler(sender, args) {
document.getElementById('<%=divDtPaging.ClientID %>').scrollLeft = document.getElementById('<%=hdnScrollPos.ClientID %>').value;
}
if (window.Sys && Sys.WebForms && Sys.WebForms.PageRequestManager) {
var prm = Sys.WebForms.PageRequestManager.getInstance()
prm.add_beginRequest(BeginRequestHandler);
prm.add_endRequest(EndRequestHandler);
}
</script>

Radio buttons not working when binded with DataList control

When i tries to bind a radio button in a datalist it becomes multiselect as its name property becomes different even when i used GroupName to be same.
How can i make it act as radio button only.
<asp:DataList ID="dlRoomNo" runat="server" RepeatColumns="4">
<ItemTemplate>
<div class="orboxfour">
<ul class="boxfour">
<li>
<asp:RadioButton ID="rdoRoomNo" GroupName="roomNo"
Text='<%#Eval("Room No")%>' runat="server" />
</li>
</ul>
</div>
</ItemTemplate>
</asp:DataList>
There's a number of suggestions in the answers to this question.
I've solved it with a bit of jQuery, though the way I did it probably isn't the best way!
In my markup, I have a script block with
function SetUniqueRadioButton(current)
{
$('input:radio').attr('checked', false);
current.checked = true;
}
and then attached the script to a radio button in my code-behind in the ItemDataBound event
String uniqueRadioButtonScript;
RadioButton radioButton;
uniqueRadioButtonScript = "javascript:SetUniqueRadioButton(this);";
if (e.Row.RowType == DataControlRowType.DataRow)
{
radioButton = (RadioButton)e.Row.FindControl("MyRadioButton");
radioButton.Attributes.Add("onclick", uniqueRadioButtonScript)
}
the best option is like this:
1. Add script
function fnrad(rbtn) {
var radioList = document.getElementsByTagName("input");
for (var i = 0 ; i < radioList.length; i++) {
if (radioList[i].type == "radio") {
radioList[i].name = 'a';
radioList[i].setAttribute("Checked","");
}
}
rbtn.setAttribute("Checked", "checked");
}
Datalist will be like this:
<asp:DataList ID="dlEmails" RepeatLayout="Flow" runat="server">
<HeaderTemplate>
<table>
<tr>
<th>Select Email Address </th>
<th>Status</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:RadioButton ID="rbtnSelect" Text='<%#Eval("Emails") %>' onclick='fnrad(this);' GroupName="a" Checked='<%#Eval("Primary") %>' runat="server" /><br />
(<asp:Label ID="lblId" runat="server" Text='<%#Eval("Verified") %>'> </asp:Label>)
</td>
<td valign="middle">
<asp:Label ID="Label2" Style="display: none;" runat="server" Text='<%#Eval("Id") %>'> </asp:Label>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("Main") %>'> </asp:Label>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:DataList>

Categories

Resources