Asp controls Id generation inside repeater - asp.net

I define some controls inside repeater itemtemplate, the problem is with the Id that are generated automatically.
This is my page:
<asp:Repeater ID="rptThreads" runat="server"
onitemcreated="rptThreads_ItemCreated">
<HeaderTemplate>
<table cellpadding="0px" cellspacing="0">
</HeaderTemplate>
<ItemTemplate>
<tr style="height:50px">
<td>
<asp:PlaceHolder ID="plcItemTitle" runat="server">
<asp:Panel id="titleContainer" runat="server" style="position:absolute;">
<asp:HyperLink ID="lnkTitle" runat="server" style="float:left;padding-right:10px;" Text='<%# Container.DataItem%>'/>
<asp:Panel id="pnlEditButtons" runat="server" Visible="false" style="vertical-align:middle;z-index:100;display:none;float:left;" >
<asp:ImageButton ID="imgbtn1" runat="server" ImageUrl="~/Images/misc/edit.png" />
<asp:ImageButton ID="imgbtn2" runat="server" ImageUrl="~/Images/misc/Rename.png" />
</asp:Panel>
</asp:Panel>
</asp:PlaceHolder>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
Now I will try to describe the problem:
code-behind:
protected void Page_Load(object sender, EventArgs e)
{
int [] array = {1,2,3,4,5};
rptThreads.DataSource = array;
rptThreads.DataBind();
}
protected void rptThreads_ItemCreated(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
Panel editButtonsPanel = e.Item.FindControl("pnlEditButtons") as Panel;
editButtonsPanel.Visible = true;
Panel containerPanel = e.Item.FindControl("titleContainer") as Panel;
//Point of Interest!!!!
containerPanel.Attributes.Add("onmouseover", "ShowEditButtons('" + editButtonsPanel.ClientID + "');");
}
}
If I run the page as is, the generated html will be the following (I show only the first 2 items):
<table cellpadding="0px" cellspacing="0">
<tr style="height:50px">
<td>
<div id="titleContainer" onmouseover="ShowEditButtons('pnlEditButtons');" style="position:absolute;">
<a id="lnkTitle" style="float:left;padding-right:10px;">1</a>
<div id="pnlEditButtons" style="vertical-align:middle;z-index:100;display:none;float:left;">
<input type="image" name="imgbtn1" id="imgbtn1" src="Images/misc/edit.png" style="border-width:0px;" />
<input type="image" name="imgbtn2" id="imgbtn2" src="Images/misc/Rename.png" style="border-width:0px;" />
</div>
</div>
</td>
</tr>
<tr style="height:50px">
<td>
<div id="titleContainer" onmouseover="ShowEditButtons('pnlEditButtons');" style="position:absolute;">
<a id="lnkTitle" style="float:left;padding-right:10px;">2</a>
<div id="pnlEditButtons" style="vertical-align:middle;z-index:100;display:none;float:left;">
<input type="image" name="imgbtn1" id="imgbtn1" src="Images/misc/edit.png" style="border-width:0px;" />
<input type="image" name="imgbtn2" id="imgbtn2" src="Images/misc/Rename.png" style="border-width:0px;" />
</div>
</div>
</td>
</tr>
As you can see all divs get the SAME ID, THIS I DONT WANT!!!
But If I omit this line form the ItemCreated event:
containerPanel.Attributes.Add("onmouseover", "ShowEditButtons('" + editButtonsPanel.ClientID + "');");
The generated HTML will be the following:
<table cellpadding="0px" cellspacing="0">
<tr style="height:50px">
<td>
<div id="rptThreads_ctl01_titleContainer" style="position:absolute;">
<a id="rptThreads_ctl01_lnkTitle" style="float:left;padding-right:10px;">1</a>
<div id="rptThreads_ctl01_pnlEditButtons" style="vertical-align:middle;z-index:100;display:none;float:left;">
<input type="image" name="rptThreads$ctl01$imgbtn1" id="rptThreads_ctl01_imgbtn1" src="Images/misc/edit.png" style="border-width:0px;" />
<input type="image" name="rptThreads$ctl01$imgbtn2" id="rptThreads_ctl01_imgbtn2" src="Images/misc/Rename.png" style="border-width:0px;" />
</div>
</div>
</td>
</tr>
<tr style="height:50px">
<td>
<div id="rptThreads_ctl02_titleContainer" style="position:absolute;">
<a id="rptThreads_ctl02_lnkTitle" style="float:left;padding-right:10px;">2</a>
<div id="rptThreads_ctl02_pnlEditButtons" style="vertical-align:middle;z-index:100;display:none;float:left;">
<input type="image" name="rptThreads$ctl02$imgbtn1" id="rptThreads_ctl02_imgbtn1" src="Images/misc/edit.png" style="border-width:0px;" />
<input type="image" name="rptThreads$ctl02$imgbtn2" id="rptThreads_ctl02_imgbtn2" src="Images/misc/Rename.png" style="border-width:0px;" />
</div>
</div>
</td>
</tr>
All divs get unique IDs, and this I do want
My questions are:
1)why it happens? why this line of code messup the ids?
2)how can have the unique ID's and assign javascript in codebehind?
I can add this on aspx (it will wotk and I will get unique ids):
onmouseover='<%# "javascript:ShowEditButtons(\""+ Container.FindControl("pnlEditButtons").ClientID+ "\");" %>'
But I must do it in codebehind because I need to set the javascript only if server validate some things.

Well as to why this is happening I'm not real sure. I suspect it may know that you used the ClientID so it didn't change it according to the naming container when the HTML was rendered.
As to what you can do to fix the problem, don't pass the ID to the javascript function. When an event fires in javascript the event object will be passed to the function for Firefox, IE has an explicit windows.event object. The event object will have a reference to the object that fired the event which you can then use to access the ID, but my guess is you were going to use the ID to get a reference to the element anyway.

An odd problem, can't say why this is happening, BUT... try moving this code to ItemDataBound instead of ItemCreated, I think you'll have more luck. I've written code exactly like this, but using OnItemDataBound and not had a problem.
Theoretically, any control inside a NamingContainer should get a unique ID, so there is definitely something fishy going on here.

Related

How to Get Selected Cell Value InTextBox

I have a textbox and a GridView. I have made a column in gridview as a linkbutton. I want that when I click a linkbutton I get that link button text value in my textbox.
My gridview is shown below-
I want When I click test5 my textbox1.text = test5 and so on..
How can I do this ?
Please add commandname in gridview link column as below
<asp:LinkButton runat="server" id="lnklink" CommandName="displayLink" />
And add the "RowCommand" event on gridview like below
<asp:GridView ID="gvDemo" runat="server
onrowcommand="gvDemo_RowCommand" />
protected void gvDemo_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "displayLink")
{
textbox1.text=((LinkButton)e.CommandSource).Text;
}
}
You can do this both in asp as well as using client side technologies like JQUERY javascript etc, I will suggest you to use jquery since keeping such things in client side will help you improve the performance
Now when u create a grid /repeater etc ultimately .net renders it into simple html controls, and jquery or client side technologies just work over this html , here is a small example with simple html
> HTML
My data : <input type="Text" id="TxtBox"/> //in .net id u have to take care by using .clientid property
<table>
<tr>
<td width="50">
<input type="checkbox"/>
</td>
<td width="50">
SOmething
</td>
<td width="50">
<a href="#" class="someName" >LINK1</a>
</td>
</tr>
<tr>
<td width="50">
<input type="checkbox"/>
</td>
<td width="50">
SOmething2
</td>
<td width="50">
<a href="#" class="someName" >LINK2</a> //class name is important since thats what we are going deal with
</td>
</tr>
<tr>
<td width="50">
<input type="checkbox"/>
</td>
<td width="50">
SOmething3
</td>
<td width="50">
LINK3
</td>
</tr>
</table>
JQUERY
$(".someName").on("click","",function(){
$("#TxtBox").val($(this).text());
});
FIDDLE
http://jsfiddle.net/AmarnathRShenoy/Hw4UG/
create RowCommand event for grid and then do the following code
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.ToLower() == "test")
{
textbox1.Text = e.CommandArgument.ToString();
}
}
and for aspx page do the following in your linkbutton
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="linkbtn" runat="server" Text="test5" CommandArgument="test5" CommandName="test" />
</ItemTemplate>
</asp:TemplateField>
or if you are binding your database table column to your linkbutton then do the following
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="linkbtn" runat="server" Text='<% yourcolumname %>' CommandArgument='<% yourcolumname %>' CommandName="test" />
</ItemTemplate>
</asp:TemplateField>

How to retrieve ID of button clicked within usercontrol on Asp.net page?

I have a page that I am working on that I'm linking multiple user controls to. The user control contains 3 buttons, an attach, clear and view button. When a user clicks on any control on the page, the resulting information is "dumped" into the last visible control on the page.
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" MasterPageFile="DefaultPage.master" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
<%# Register tagName="FileHandler" src="FileHandling.ascx" tagPrefix="ucFile" %>
<asp:Content ID="Content1" ContentPlaceHolderID="Main" Runat="Server">
<asp:UpdatePanel ID="upPanel" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<table>
<tr>
<td>
<ucFile:FileHandler ID="fFile1" runat="server" />
</td>
<td>
<ucFile:FileHandler ID="fFile2" runat="server" />
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
All file handling and processing is handled within the control, with an event when the upload to the file server is complete via a file name that was generated. When either button is clicked, the file name is always stored internal to the control in the last control's text box.
Control code:
<table style="width: 50%;">
<tr style="white-space: nowrap;">
<td style="width: 1%;">
<asp:Label runat="server" ID="lblFile" />
</td>
<td style="width: 20%;">
<asp:TextBox ID="txtFile" CssClass="backColor" runat="server" OnTextChanged="FileInformationChanged" />
</td>
<td style="width: 1%">
<%--<asp:Button runat="server" ID="btnUpload" CssClass="btn" Text="Attach" OnClick="UploadFile"/>--%>
<input type="button" id="btnUpload" class="btn" tabindex="30" value="Attach" onclick="SetupUpload();" />
</td>
<td style="width: 1%">
<%--<asp:Button runat="server" ID="btnClear" Text="Clear" CssClass="btn" OnClick="ClearTextValue"/>--%>
<input type="button" id="btnClearFile" class="btn" value="Clear" onclick="document.getElementById('<%=txtFile.ClientID%>').value = '';document.getElementById('<%=hfFile.ClientID%>').value = '';" />
</td>
<td style="width: 1%">
View
</td>
<td style="width: 1%">
<asp:HiddenField ID="hfFile" runat="server" />
</td>
</tr>
</table>
<script type="text/javascript">
var ItemPath = "";
function SetupUpload(File) {
ItemPath = File;
VersionAttach('<%=UploadPath%>', 'true');
}
function UploadComplete(File) {
document.getElementById('<%=txtFile.ClientID%>').value = File.substring(File.lastIndexOf("/") + 1);
document.getElementById('<%=hfFile.ClientID%>').value = File;
alert('<%=txtFile.Text %>');
alert('<%=ClientID %>')
}
function ViewLink(File, Alert) {
if (File != "") {
if (File.indexOf("../data/") != -1) {
window.open(File, '_blank');
}
else {
window.open('../data/<%=UploadPath%>/' + File, '_blank');
}
}
else if (Alert == "") {
alert('No file has been uploaded for this field.');
}
}
</script>
There are a couple of ways
a) use control.FindControl("hfFile")
b)
private HiddenField hfFile;
protected void hfFile_OnInit(object sender, EventArgs e){
hfFile = (HiddenField)sender;
}
The way I got this to work in my case was to put a title on the input button (client control), then passing the value of the clientid of the control that I wanted (textbox in this case) to the function in the onclick event (client event). I then "force" the consumer of the control to use this information when the button is clicked. The reason for the question here was to find out if there is a better way of accomplishing this within the control instead of making the consumer of the control handle the data. This does work for my purpose, it just doesn't seem as clean as I would have liked.

ASP.Net Refresh GridView

Afternoon All,
I am using a visual studio 2010. I have a web page which is used to record minutes of meetings. The page has a section that the user can use to add 'Actions' to the site. I can get a user to successfully add an action to the page.
The issue that i have is i also have a grid view on the webpage and would like this to refresh once the user has added the new 'Action' to the page. So that the user can see that its been submitted.
Im new ish to the .net environment and VB and im not sure 100% how to complete this task.
I have the following code from my .aspx page....
Submitted Actions:
<hr />
<!-- DataSource for submitted Actions -->
<asp:SqlDataSource ID="OutstandingActionsDS" runat="server"
ConnectionString="<%$ ConnectionStrings:SMCConnectionString %>"
SelectCommand="Populate_grdOutstandingActions"
SelectCommandType="StoredProcedure"></asp:SqlDataSource>
<!-- Gridview that holds submitted Actions -->
<asp:GridView ID="GridView1" runat="server" DataSourceID="OutstandingActionsDS">
</asp:GridView>
<br />
<br />
New Actions to Record:
<hr />
<!-- Add new Action -->
<asp:Panel ID="pnlHeaderAction" runat="server" CssClass="pnl" Width="740px">
<div style="float:left;">
Record New Actions
</div>
<div style="float:right;">
<asp:Label ID="lblShowHideAction" runat="server" ></asp:Label>
</div>
<div style="clear:both"></div>
</asp:Panel>
<asp:Panel ID="pnlInfoAction" runat="server" CssClass="pnlBody">
<table>
<tr>
<td style="width:498px; height: 15px;"><h5>Actions:</h5></td>
<td style="width:130px; height: 15px;"><h5>Owner:</h5></td>
<td style="height: 15px;"><h5> Target Date:</h5></td>
</tr>
</table>
<table style="width: 99%">
<tr>
<td style="width: 495px">
<asp:TextBox ID="txtAction" runat="server" TextMode="MultiLine"
Width="493px" Height="50px" style="font-family:Verdana"></asp:TextBox>
</td>
<td style="width: 132px" valign="top">
<asp:TextBox ID="txtOwner" runat="server" Height="50px"
width="128px" style="font-family:Verdana"></asp:TextBox>
</td>
<td valign="top">
<asp:TextBox ID="txtTargetDate" runat="server" width="89px" style="font-family:Verdana"></asp:TextBox>
</td>
</tr>
</table>
<br />
<div style="text-align: right;">
<asp:Button ID="btnAddAction" runat="server" Text="Add New Action" CssClass="button" />
</div>
</asp:Panel>
Here is my code for the VB page...
Protected Sub btnAddAction_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddAction.Click
Dim oActionClass As New ActionClass
With oActionClass
.Action = txtAction.Text
.Owner = txtOwner.Text
.TargetDate = New SmartDate(Convert.ToDateTime(txtTargetDate.Text))
oActionClass.ActionID = ActionClassDAL.AddActionClass(oActionClass)
ClearActions()
End With
End Sub
Private Sub ClearActions()
txtAction.Text = ""
txtOwner.Text = ""
txtTargetDate.Text = ""
End Sub
This seems like a simple request but i cant seem to find anything that shows me how to refresh the grid based on the user adding the action to the system.
Many thanks in advance for any help offered.
Regards
Betty
you just have to put a databind to the GridView1. I think is something like this:
me.GridView1.databind();
On the bottom of your method btnAddAction_Click.
I hope I have been helpful.

ASP.NET Enter key

I have a master page and based on that master page I created a page with a TextBox and two Validation controls for RequiredValidator and RegularExpressionValidator and one ValidationSummary.
when pressing the enter key in TextBox, I expected that both validator show their errorMessage on validationSummary but thats not what happening it only works when I press the button on page.
According to this page I wrapped my code with <asp:panel> and a DefaultButton attribute but it didn't solve the problem.
http://www.codeproject.com/KB/aspnet/aspnet_Enter_key_problem.aspx
I want to know what the problem here is and whether there is any workaround?
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
**<asp:Panel runat="server" DefaultButton="EmailSend">**
<table style="width: 100%">
<tr>
<asp:ValidationSummary ID="EmailValidation" runat="server" CssClass="failureNotification"
ValidationGroup="EmailValidation" />
</tr>
<tr>
<td style="width: 76px">
Email:
</td>
<td class="style1">
<asp:TextBox ID="EmailForget" runat="server" Width="244px"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="EmailRequiered" runat="server" ControlToValidate="EmailForget"
CssClass="failureNotification" ErrorMessage="RequiredFieldValidator" ValidationGroup="EmailValidation">*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="EmailRegular" runat="server" ControlToValidate="EmailForget"
CssClass="failureNotification" ErrorMessage="email required"
ValidationGroup="EmailValidation">*</asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td style="width: 76px">
</td>
<td class="style1">
<asp:Button ID="EmailSend" runat="server" Text="send" Width="56px" ClientIDMode="Static" />
</td>
<td>
</td>
</tr>
</table>
**<asp:Panel>**
</asp:Content>
Try adding this code
<script type="text/javascript">
if (document.addEventListener) {//if Firefox
document.addEventListener("keypress", fireFoxHandler, true);
} else {
document.attachEvent("onkeyup", ieHandler);
}
function fireFoxHandler(evt) {
if (evt.keyCode == 13) {
document.getElementById("EmailSend").click();
}
}
function ieHandler(evt) {
if (evt.keyCode == 13) {
document.getElementById("EmailSend").click();
}
}
</script>
Found this solution here.
(I didn't check this in MasterPage scenario, please check...)
If you are using IE then according to the answers on these two pages:
ASP.NET enter key and form submit with no javascript
Enter button does not submit form (IE ONLY) ASP.NET
you only need to add the following to your form
<!-- Fix for IE bug submit on pressing "Enter") -->
<div style="display:none">
<input type="text" name="hiddenText"/>
</div>
I solved this problem like this:
protected void Page_Load(object sender, EventArgs e)
{
textboxusername.Attributes.Add("onKeyPress", "javascript:if (event.keyCode == 13) __doPostBack('" + LinkButton1.UniqueID + "','')");
textboxpassword.Attributes.Add("onKeyPress", "javascript:if (event.keyCode == 13) __doPostBack('" + LinkButton1.UniqueID + "','')");
}
and this is the link..
<asp:LinkButton ID="LinkButton1" runat="server" class="btn" OnClick="LinkButton1_Click">

asp.net radio button grouping

I am currently having an issue with radio buttons and grouping. I have an asp radio button within a repeater control. I have the group name attribute set to "Customer". When the page loads, the radio buttons are not grouped. Instead of the id fields being set to the group name, it is setting the value fields of the radio buttons. I know that I have tried setting radio buttons up outside of a repeater control and have had the same issue. What is going on here?
aspx
<asp:Repeater ID="repCustomers" runat="server">
<HeaderTemplate>
<table class="tableDefault" cellpadding="0" cellspacing="0" border="0" style="width: 383px; border: 0px !important">
<tr>
<th> </th>
<th>Cust. No.</th>
<th>Cust. Name</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:RadioButton ID="radCustomer" GroupName="Customer" runat="server" ValidationGroup="Customer" ToolTip='<%#Eval("CustomerNumber") %>' />
</td>
<td><%#Eval("CustomerNumber")%></td>
<td><%#Eval("Name") %></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
output html
<table class="tableDefault" cellpadding="0" cellspacing="0" border="0" style="width: 383px; border: 0px !important">
<tr>
<th> </th>
<th>Cust. No.</th>
<th>Cust. Name</th>
</tr>
<tr>
<td>
<span title="111111"><input id="ctl00_PrimaryContent_repCustomers_ctl01_radCustomer" type="radio" name="ctl00$PrimaryContent$repCustomers$ctl01$Customer" value="radCustomer" /></span>
</td>
<td>111111</td>
<td>Jeremy's Test</td>
</tr>
<tr>
<td>
<span title="222222"><input id="ctl00_PrimaryContent_repCustomers_ctl02_radCustomer" type="radio" name="ctl00$PrimaryContent$repCustomers$ctl02$Customer" value="radCustomer" /></span>
</td>
<td>222222</td>
<td>My Test</td>
</tr>
<tr>
<td>
<span title="333333"><input id="ctl00_PrimaryContent_repCustomers_ctl03_radCustomer" type="radio" name="ctl00$PrimaryContent$repCustomers$ctl03$Customer" value="radCustomer" /></span>
</td>
<td>333333</td>
<td>Jim Bob's BBQ</td>
</tr>
<tr>
<td>
<span title="444444"><input id="ctl00_PrimaryContent_repCustomers_ctl04_radCustomer" type="radio" name="ctl00$PrimaryContent$repCustomers$ctl04$Customer" value="radCustomer" /></span>
</td>
<td>444444</td>
<td>New Hope Hamburgers</td>
</tr>
<tr>
<td>
<span title="555555"><input id="ctl00_PrimaryContent_repCustomers_ctl05_radCustomer" type="radio" name="ctl00$PrimaryContent$repCustomers$ctl05$Customer" value="radCustomer" /></span>
</td>
<td>555555</td>
<td>Pied Piper Pizza</td>
</tr>
<tr>
<td>
<span title="666666"><input id="ctl00_PrimaryContent_repCustomers_ctl06_radCustomer" type="radio" name="ctl00$PrimaryContent$repCustomers$ctl06$Customer" value="radCustomer" /></span>
</td>
<td>666666</td>
<td>Sandy's Subs</td>
</tr>
<tr>
<td>
<span title="777777"><input id="ctl00_PrimaryContent_repCustomers_ctl07_radCustomer" type="radio" name="ctl00$PrimaryContent$repCustomers$ctl07$Customer" value="radCustomer" /></span>
</td>
<td>777777</td>
<td>Leonard's Lambchops</td>
</tr>
<tr>
<td>
<span title="888888"><input id="ctl00_PrimaryContent_repCustomers_ctl08_radCustomer" type="radio" name="ctl00$PrimaryContent$repCustomers$ctl08$Customer" value="radCustomer" /></span>
</td>
<td>888888</td>
<td>Dave's Diamond Deli</td>
</tr>
<tr>
<td>
<span title="999999"><input id="ctl00_PrimaryContent_repCustomers_ctl09_radCustomer" type="radio" name="ctl00$PrimaryContent$repCustomers$ctl09$Customer" value="radCustomer" /></span>
</td>
<td>999999</td>
<td>Ernie's Eatery</td>
</tr>
</table>
I finally got around this by creating a plain radio button and setting the value using an server-side eval.
<input type="radio" name="radCustomer" value='<%#Eval("CustomerNumber") %>' />
Now when the application performs a postback, I check for the value of Request.Form["radCustomer"]. This works flawlessly.
Unfortunately, this is a well known issue with radio buttons within a repeater. One of your only options would be to create a custom server control derived from the RadioButton class and override how it renders.
EDIT: Here's a sample of what the derived class may look like:
public class MyRadioButton : RadioButton
{
protected override void Render(HtmlTextWriter writer)
{
writer.Write("<input id=\"" + base.ClientID + "\" ");
writer.Write("type=\"radio\" ");
writer.Write("name=\"" + base.ID + "\" ");
writer.Write("value=\"" + base.ID + "\" />");
writer.Write("<label for=\"" + base.ClientID + "\">");
writer.Write(base.Text);
writer.Write("</label>");
}
}
I fixed it in javascript:
$(function () {
$("#divWithGridViewOrRepeater input:radio").attr("name", "yourGroupName");
});
I had the same issues. I am using Literal as placeholder to render radio button onItemCreated event.
ASP.Net
<asp:Repeater ID="rpt" runat="server" OnItemCreated="rpt_OnItemCreated">
<ItemTemplate>
<asp:Literal ID="lit" runat="server"></asp:Literal>
</ItemTemplate>
</asp:Repeater>
C#
protected void rpt_OnItemCreated(object sender, RepeaterItemEventArgs e) {
Literal lit = (Literal)e.Item.FindControl("lit");
lit.Text = "<input type=\"radio\" name=\"myGroup\">";
}
I had to modify slightly the answer posted above by r3dsky.
Here's what worked for me:
$(document).ready(function () {
$(".divWithGridViewOrRepeater input:radio").attr("name", "yourGroupName");
});
I would start by adding a value on my radiobutton Value='<%#Eval("CustomerNumber") %>'.
I made my radiobutton have autopostback set to true, and then in the event handler set all the other radio buttons to BE unselected.
Not ideal, but I need lots control over the visibility and enabled attributes of the radiobutton, and it seemed easier to let ASP.NET control that rather than resorting to client side script.
This is a well known bug with the ASP.NET Repeater using RadioButtons:
here best solution in my opinion
I did this:
$("input:radio").attr("name", $("input:radio").first().attr("name"));
Why? because if you replace the name property for any string you want, you will get an 'not found error'. So, you need to get the name of the first radiobutton, and rename all of them with that name. It works like a sharm ;)
My solution, similar to others:
<input id="ctlRadio" runat="server" type="radio" data-fixgroupbug="1" >
// Fixes this ASP.NET bug: if radio input is inside repeater you can't set its name.
// Every input gets set different name by ASP.NET.
// They don't behave as a group. You can select multiple radios.
function fixRadiogroupBug()
{
$('[type="radio"][data-fixgroupbug]').click(function () {
$(this).siblings('[type="radio"]').prop('checked', false);
});
}
$(document).ready(function () {
fixRadiogroupBug();
});

Resources