How to access aspx control in code behind? - c#

NET. i am trying to access the div tag in code behind which is inside the SeparatorTemplate
Here is my aspx code
<div>
<asp:DataList ID="DataList1" runat="server">
<ItemStyle ForeColor="#4A3C8C" BackColor="#E7E7FF"></ItemStyle>
<HeaderTemplate>
<table width="900px">
<tr>
<td width="300px">
<b>Name</b>
</td>
<td width="300px">
<b>Account No</b>
</td>
<td width="300px">
<b>Company</b>
</td>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<table width="900px">
<tr>
<td align="left" width="300px">
<%# DataBinder.Eval(Container.DataItem, "Name")%>
</td>
<td align="left" width="300px">
<%# DataBinder.Eval(Container.DataItem, "AccountNo")%>
</td>
<td align="left" width="300px">
<%# DataBinder.Eval(Container.DataItem, "Company")%>
</td>
</tr>
</table>
</ItemTemplate>
<HeaderStyle Font-Bold="True" ForeColor="#F7F7F7" BackColor="#4A3C8C"></HeaderStyle>
<SeparatorTemplate>
<div id="divSeprator" runat="server">//This div tag i want to access in the code behind
<br />
</div>
</SeparatorTemplate>
</asp:DataList>
</div>
I have tried accessing the this.Controls and DataList1.Controls but both of those doesn't contain this div i know it is in the SepratorTemplate but i don't know how to access control from that template because there is nothing to find the controls.

protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
// Find the div control as htmlgenericcontrol type, if found apply style
System.Web.UI.HtmlControls.HtmlGenericControl div = (System.Web.UI.HtmlControls.HtmlGenericControl)e.Item.FindControl("DivContent");
if(div != null)
div.Style.Add("border-color", "Red");
}

You will need to find it from the datalist row as below.
HtmlGenericControl div = (HtmlGenericControl)yourDataList.Items[0].FindControl("dvSeparator");
You can pass the index of the data list item (row) in .Items[] for which you want to find the div for processing.
If you want to process div from all the datalist items then you can do it in the item data bound event of the datalist as #Upvote MarkAnswer has suggested in his answer.

HtmlGenericControl divSeprator = (HtmlGenericControl)DataList1.Items[0].FindControl("divSeprator");
Where 0 is your item index.
Or just bind a DataList1_ItemDataBoud event and use:
if(e.Item.ItemType == ListItemType.Separator)
HtmlGenericControl divSeprator = (HtmlGenericControl)e.Item.FindControl("divSeprator");

you need to make tag runat="sever" and give it id
<div id="div" runat="server">
Then you can access it by using
HtmlGenericControl div = HtmlGenericControl("div")

Related

Repeater inside another repeater. access an event values

I have an repeater inside another repeater. The second repeater have an checkbox event "isChecked_OnCheckedChanged". My problem is there. When this event occurs I need to access the value of the variable "lblName" and also the value of the variable "lblID" in parent repeater.
<asp:Repeater ID="rptOne" OnItemDataBound="populateSecondRepeater" runat="server">
<HeaderTemplate>
<table s>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="lblID" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
</td>
<td style="width: 15%; vertical-align: top;">
<asp:Repeater ID="rptTwo" runat="server">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td style="width: 85%;">
<img runat="server" src='<%# Eval("Img") %>' alt="#" id="img_flag" /></td>
<td style="width: 15%; padding-right: 40px">
<asp:Label ID="lblName" runat="server" Style="display: none;" Text='<%# Eval("IDName") %>'></asp:Label>
<asp:CheckBox ID="check" runat="server" AutoPostBack="True" Checked='<%# Eval("isChecked") %>' OnCheckedChanged="isChecked_OnCheckedChanged" />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
I can access the value of the variable "lblName", but how do I access the values ​​of the parent repeater?
protected void isChecked_OnCheckedChanged(object sender, EventArgs e)
{
CheckBox chk = (sender as CheckBox);
RepeaterItem item = chk.NamingContainer as RepeaterItem;
if (item != null)
{
Label lbl = (Label) item.FindControl("lblName");
}
}
thank you.
There are multiple ways to do this. You can add something like ParentID property to your data model, set that to the value you want and bind that to a hidden field next to the lblName label.
Alternatively, use a <tr runat="server" id="rptOneRow"> for each item in rptOne, and jump up in isChecked_OnCheckedChanged from the RepeaterItem until you find such row. Then use FindControl to find the lblID.
Or see here how to access the data directly - Accessing parent data in nested repeater, in the HeaderTemplate.

How to add controls dynamically in .net?

I want to add controls dynamically
Code:
.aspx
<body>
<form id="form1" runat="server">
<asp:Label ID="lbl1" Text="Personal Information" Font-Size="Large" ForeColor="White" runat="server" Width="854px" BackColor="SteelBlue" style="margin-top: 0px" Height="60px"> </asp:Label>
<div id="div1" runat="server">
<asp:Panel ID="panelPersonal" runat="server">
<div id="divreg" runat="server">
<table id="tbl" runat="server">
<tr>
<td class="style8"> Visa Number:</td>
<td class="style20"> <asp:TextBox ID="txtUser" Width="160px" runat="server"/></td>
<td class="style22"> Country Name:</td>
<td class="style23"> <asp:DropDownList ID="dropCountry" Width="165px" runat="server"></asp:DropDownList></td>
</tr>
<tr>
<td class="style22"> Type of Visa:</td>
<td class="style23"> <asp:DropDownList ID="dropVisa" Width="165px" runat="server"> </asp:DropDownList></td>
<td class="style22"> Type of Entry:</td>
<td class="style23"> <asp:DropDownList ID="dropEntry" Width="165px" runat="server"></asp:DropDownList></td>
</tr>
<tr>
<td class="style8"> Expiry Date</td>
<td class="style20">
<%--<BDP:BasicDatePicker ID="BasicDatePicker4" runat="server" onselectionchanged="BasicDatePicker2_SelectionChanged" AutoPostBack="True" />--%>
</td>
</tr>
</table>
</div>
</asp:Panel>
<asp:Button ID="addnewtext" runat="server" Text="Add" onclick="addnewtext_Click" width="76px" />
</div>
</form>
Here I am using User control
.ascx
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="AddVisaControl.ascx.cs" EnableViewState="false" Inherits="Pyramid.AddVisaControl" %>
<div id="divreg" runat="server">
<table id="tbl" runat="server">
<tr>
<td> Visa Number:</td>
<td><asp:TextBox ID="txtUser" Width="160px" runat="server"/></td>
<td> Country Name:</td>
<td><asp:DropDownList ID="dropCountry" Width="165px" runat="server"></asp:DropDownList></td>
</tr>
<tr>
<td> Type of Visa:</td>
<td><asp:DropDownList ID="dropVisa" Width="165px" runat="server"></asp:DropDownList></td>
<td> Type of Entry:</td>
<td><asp:DropDownList ID="dropEntry" Width="165px" runat="server"></asp:DropDownList></td>
</tr>
<tr>
<td> Expiry Date</td>
<td>
</td>
</tr>
</table>
</div>
.aspx.cs
Here is the problem If I click add button one row is added successfully but when I close and open again previously added rows also coming.
I think the problem is static int i = 0;
Any other way is there?
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
}
static int i = 0;
protected void addnewtext_Click(object sender, EventArgs e)
{
i++;
for (int j = 0; j <= i; j++)
{
AddVisaControl ac = (AddVisaControl)Page.LoadControl("AddVisaControl.ascx");
PlaceHolder1.Controls.Add(ac);
PlaceHolder1.Controls.Add(new LiteralControl("<BR>"));
}
}
This is what I am getting
This is what I want, Like below image I want to do
Any ideas? Thanks in advance
can you use a repeater /gridview control in the place of place holder control ,i think that is the best method for your situvation
I recommend this solution. It is different from the code you are using but still very easy.
You can use a Repeater control
<asp:Repeater ID="rpt1" runat="server">
<ItemTemplate>
user control code goes here but first put it in a div(example id - div1) with `style="display:none"`
</ItemTemplate>
</asp:Repeater>
on the server side you do this
rpt1.DataSource = Utils.GetRptTable(4); // will add the code 4 times but it will not be visible
rpt1.DataBind();
Later in javascript on the page you can find the first div with id div1 that has style - display:none and show it. That way you will show the next "row" with fields.
Edit 1
If you can add all your fields in the user control in a single <tr> you don't need a div with style= display:none. You will just have to add this class="GridBody" to your <tbody> - like: <tbody class="GridBody">
Then you do this to the row:
<tr class="GridRow" id="row1" style="display: none">
...
</tr>
and the javascript to show the row is:
function addRow() {
var body = $(".GridBody");
var indexOfNextRow = $('tr[class="GridRow"][hidden="false"]').length;
var tr = $(".GridBody tr")[indexOfNextRow];
tr.style.display = 'table-row';
tr.setAttribute('hidden', 'false');
}

asp net repeater radio button group name and get radio button value

I have an aspx c# page for bank account numbers and credit card installment list in one page
if user want to bank transfer it choose bank account else if user want to pay credit card installment it did it.
the problem is radio buttons group name. I didn't group the radio buttons and didn't get the selected radio button value.
Here is my codes.
This İs Bank Account List
<asp:Repeater ID="RptBankalar" runat="server">
<ItemTemplate>
<div class="BankaListBox">
<table width="100%" height="100" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="200" align="center" valign="middle">
<img alt="<%#Eval("BankName").ToString() %>" src="../../Files/<%#Eval("Logo").ToString() %>" /></td>
<td>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="120" height="22"><strong>Account Owner</strong></td>
<td width="15"><strong>:</strong></td>
<td><%#Eval("AcOwner").ToString() %></td>
</tr>
<tr>
<td width="120" height="22"><strong>Branch No</strong></td>
<td width="15"><strong>:</strong></td>
<td><%#Eval("Branch").ToString() %></td>
</tr>
<tr>
<td width="120" height="22"><strong>Account Number</strong></td>
<td width="15"><strong>:</strong></td>
<td><%#Eval("AccountNum").ToString() %></td>
</tr>
<tr>
<td width="120" height="22"><strong>IBAN</strong></td>
<td width="15"><strong>:</strong></td>
<td><%#Eval("Iban").ToString() %></td>
</tr>
</table>
</td>
<td width="100" align="center" valign="middle">
<asp:RadioButton ID="RadioBtn" Text='<%#Eval("id").ToString() %>' runat="server" />
</td>
</tr>
</table>
</div>
</ItemTemplate>
</asp:Repeater>
Same Page Credit card installment list
<asp:Repeater ID="RptTaksitList" OnItemDataBound="RptTaksitList_ItemDataBound" runat="server">
<ItemTemplate>
<tr runat="server" id="arka_tr">
<td align="center" height="22" width="40" runat="server" id="td1">
<b style="display: none; visibility: hidden">
<asp:Literal ID="lt_id" Text='<%#Eval("BankaID").ToString() %>' runat="server"></asp:Literal>
</b>
<asp:Literal ID="lt_taksit_sayisi" Text='<%#Eval("TaksitSayisi").ToString() %>' runat="server"></asp:Literal></td>
<td width="150" runat="server" id="td2">
<asp:Literal ID="LblSepetTotal" runat="server" Text='<%#Session["SepetUrunToplami"] %>'></asp:Literal></td>
<td runat="server" id="td3"><b style="display: none; visibility: hidden">
<asp:Literal ID="lt_komisyon_orani" Text='<%#Eval("KomisyonOrani").ToString() %>' runat="server"></asp:Literal>
</b>
<asp:Literal ID="lt_toplam_tutar" runat="server" Text=''></asp:Literal>
<b style="display: none; visibility: hidden">
<asp:Literal ID="lt_alt_limit" Text='<%#Eval("AltLimit").ToString() %>' runat="server"></asp:Literal>
</b>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
How can i get the values and grouping this radio buttons.
Normally i did in classic asp but i change my coding language.
Kindly regards.
Thanks for your helps.
i never help in Stack. So i will contribute for the first time after been helped so many times by the community.
I hove you like. I have a more simple solution.
Surround your radio button with some element (DIV) with a className that you might like just to pick all the radio in jQuery.
EDITED IN: The aspnet wasnt persisting the viewstate when i changed that way. Now it works just fine :)
$(document).ready(function () {
$(".radiofix").each(function () {
var objRadiosContainer = $(this);
objRadiosContainer.find("input[type=radio]").each(function () {
var objRadio = $(this);
objRadio.change(function () {
if(objRadio.get(0).checked)
{
objRadiosContainer.find("input[type=radio]").each(function () {
if($(this).get(0).id != objRadio.get(0).id)
{
$(this).get(0).checked = false;
}
});
}
});
});
});
});
Let's take a look in the HTML file (or ASPX file):
<asp:Repeater ID="questions" runat="server">
<ItemTemplate>
<h3>%# getQuestionNumber() %>) <%# Eval("questionDescription").ToString() %></h3>
<div class="nameOfTheClass">
<asp:Repeater ID="answers" runat="server">
<ItemTemplate>
<asp:RadioButton ID="radioId" runat="server" />
</ItemTemplate>
</asp:Repeater>
</div>
</ItemTemplate>
This you will work just fine. Just create some javascript file (.js) and call it, fixTheRadios.js and choose a name for "nameOfTheClass" that you like.
Important: I didnt tested the code inside an update panel. I suggest you do that.
Here's how I do it.
In the markup I declare the radio button as server side html control:
<input id="rbMyRadio" type="radio"runat="server" class="myClass" />
And in the code access the value:
foreach (RepeaterItem item in RptBankalar.Items)
{
if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
{
HtmlInputRadioButton rbMyRadio = (HtmlInputRadioButton)item.FindControl("rbMyRadio");
if (rbMyRadio != null && rbMyRadio.Checked)
{
//Do tasks
}
}
}
Grouping is done using jquery in page head:
<script type="text/javascript">
$(document).ready(function () {
$('input[type=radio]').click(function () {
var cname = $(this).attr('class');
$('.' + cname).each(function () {
$(this).prop('checked', false);
});
$(this).prop('checked', true);
});
});
</script>
And reference jquery in the head:
<script src="Scripts/jquery-1.8.2.js"></script>
Very late answer but you can also try adding the radio buttons like this in the repeater:
<asp:TemplateColumn>
<ItemTemplate>
<div style="text-align: center">
<input type="radio" id="rptMyRadio" runat="server" />
</div>
</ItemTemplate>
</asp:TemplateColumn>
Then in a script block add:
$('input[type=radio]').click(function () {
$('input[type=radio]').removeAttr("checked");
$(this).prop('checked', true);
});

How to get count of nested repeater items

I've a repeater on my page. And it's containing a nested repeater on it's item.
It's structure is as below
<asp:Repeater ID="rptOuterRepeater" runat="server" >
<ItemTemplate>
<tr >
<td>
// Need some code logic here for counting
</td>
</tr>
<tr>
<td>
<asp:Repeater ID="rptInnerRepeater" unat="server">
<ItemTemplate>
<tr >
<td>
</td>
</tr>
</ItemTemplate>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
I need to count rptInnerRepeater 's item count in place of comments. Is this possible using Inline Code.
Since, you are binding your Repeater to a Datasource, Why cannot you manipulate your datasource to contain the Count of the SubItems Collection to which your inner repeater is bound?
You can do it this way:
<asp:Repeater ID="rptOuterRepeater" runat="server">
<ItemTemplate>
<table>
<tr>
<td>
<%# Eval("Count") %>
</td>
</tr>
<tr>
<td>
<asp:Repeater ID="rptInnerRepeater" runat="server"
DataSource='<%# Eval("Items") %>'>
<ItemTemplate>
<tr>
<td>
<%# Eval("Id") %>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
and your DataSource is :
var datasource = testList.Select(s =>
new
{
Count = s.Items.Count,
Items = s.Items,
Id = s.Id
}).ToList();
rptOuterRepeater.DataSource = datasource;
rptOuterRepeater.DataBind();
so why not modify your datasource collection to contain an extra field called Count?
on the client side, you will have to use very ugly javascript/jQuery code, which will be superbly specific to your need.
Best I could come with in terms of short coding, using a literal and a ItemDataBound event handler. Not really what I would call "inline", but I hope this will help anyway.
<script runat="server">
void innerRpItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.DataItem != null)
{
var lit = (Literal) ((Control) sender).Parent.FindControl("count");
lit.Text = string.IsNullOrWhiteSpace(lit.Text) ? "1" : (int.Parse(lit.Text) + 1).ToString();
}
}
</script>
<asp:Repeater ID="rptOuterRepeater" runat="server" >
<ItemTemplate>
<tr >
<td>
// Need some code logic here for counting
<asp:Literal runat="server" ID="count"/>
</td>
</tr>
<tr>
<td>
<asp:Repeater ID="rptInnerRepeater" runat="server" OnItemDataBound="innerRpItemDataBound" >
<ItemTemplate>
<tr >
<td>
</td>
</tr>
</ItemTemplate>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
---------------- previous non working solution
You may try to replace this line
// Need some code logic here for counting
with this one
<%#((Repeater)Container.FindControl("rptInnerRepeater")).Items.Count%>
Not tested, and not really sure it would work, but worth a try I would say :-)
Hope this will help

Get value inside nested repeater in dialog

I have a problem with getting the new value from the textbox inside a nested repeater. If i type static value into the Text property like this: i can get the value, but not the new value.
<ItemTemplate>
<tr>
<td width="160">
<%# Eval("index")%>
</td>
<td>
<%# Eval("Sex") %>
</td>
<td align="right">
<button id="EditPuppy" class="open-dialog" runat="server">
Rediger hvalp</button>
<juice:Button TargetControlID="EditPuppy" runat="server" />
</td>
<td align="right" width="30">
<asp:ImageButton runat="server" ID="DeletePuppy" CommandArgument='<%# Eval("dogid").ToString() %>'
OnClientClick='return confirm("Er du sikker på at du gerne vil slette denne hvalp?")'
OnCommand="DeletePuppy_Command" SkinID="DefaultDeleteButton" />
</td>
</tr>
<div id="_Default" runat="server" class="basic-dialog" title="Basic dialog" runat="server">
<asp:TextBox runat="server" ID="TextBoxPuppyName" Text="HEJ" /><!-- The textbox i am trying to get the value from -->
<asp:Button UseSubmitBehavior="false" runat="server" ID="ButtonPuppyName" CommandArgument="<%# ((RepeaterItem)Container.Parent.Parent).ItemIndex %> <-- Getting parent repaterid"
CommandName="<%# Container.ItemIndex %> <-- Getting current repeater index -->"
OnCommand="ButtonPuppyName_Command" Text="Opdater" />
</div>
<juice:Dialog TargetControlID="_Default" AutoOpen="false" runat="server" />
</ItemTemplate>
Codebehind:
protected void ButtonPuppyName_Command(object sender, CommandEventArgs e) {
int parentRepeaterItemIndex = Convert.ToInt32(e.CommandArgument);
int childRepeaterItemIndex = Convert.ToInt32(e.CommandName);
Repeater childReapter = (Repeater)RepeaterShowKuldUserList.Items[parentRepeaterItemIndex].FindControl("RepeaterShowKuldPuppyList");
TextBox name = (TextBox)childReapter.Items[childRepeaterItemIndex].FindControl("TextBoxPuppyName");
HttpContext.Current.Response.Write(name.Text);
}
Thanks for your help!. Remember getting into the repeaters work because i can get the "static" value
I think your problem is that you are calling repeater.DataBind (in the page load?) try to add !IsPostBack and then bind your data...
other option is that the viewState=false.. it should be "true"
Hope that helps,
Ran

Categories

Resources