Multiple radiobutton columns in gridview - asp.net

I have multiple radio buttons columns in my gridview, I want to select one column at a time

As i got, you want to check only one radio button in a row so just add an attribute
GroupName with same value to all radio buttons and it will work..
e.g
<asp:TemplateField HeaderText="More Than 20% Estimate" >
<ItemTemplate >
<asp:RadioButton ID="rdbGVRow8" GroupName ="Program" onclick="javascript:CheckOtherIsCheckedByGVIDMore(this);" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="10% to 20% overestimate" >
<ItemTemplate >
<asp:RadioButton ID="rdbGVRow7" GroupName ="Program" onclick="javascript:CheckOtherIsCheckedByGVIDMore(this);" runat="server" />
</ItemTemplate>
</asp:TemplateField>
.
.
.
.
where program is a value you can give your own value but remember same value to all radio buttons.

This rather lengthy tutorial is the best I've seen that solves this issue
Adding a GridView Column of Radio Buttons
http://www.asp.net/data-access/tutorials/adding-a-gridview-column-of-radio-buttons-cs
If you are using the same group name for your buttons and they still are not working:
"The reason the radio buttons are not grouped is because their rendered name attributes are different, despite having the same GroupName property setting." If you view the source it might look something like this:
<input id="ctl00_MainContent_Suppliers_ctl02_RowSelector"
name="ctl00$MainContent$Suppliers$ctl02$SuppliersGroup"
type="radio" value="RowSelector" />
<input id="ctl00_MainContent_Suppliers_ctl03_RowSelector"
name="ctl00$MainContent$Suppliers$ctl03$SuppliersGroup"
type="radio" value="RowSelector" />
The fix is to use literal controls to inject the name in.
protected void Suppliers_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Grab a reference to the Literal control
Literal output = (Literal)e.Row.FindControl("RadioButtonMarkup");
// Output the markup except for the "checked" attribute
output.Text = string.Format(
#"<input type="radio" name="SuppliersGroup" " +
#"id="RowSelector{0}" value="{0}" />", e.Row.RowIndex);
}
}

Related

MaskedEditExtender is not working from 2nd line in gridview in Asp.Net

I have a gridview on my page, there is three columns, one is for quantity, i need to use MaskEditExtender with simple TextBox, but it is working only on first row, from second row the textbox is appearing without mask.
here is my code :
<Columns>
<asp:TemplateField HeaderText="Qty">
<ItemTemplate>
<asp:TextBox ID="txtQuan" runat="server" ValidationGroup="MKE" MaxLength="5"
style="text-align:right" width="100px" Text='<%# BIND("QUAN") %>' />
<ajaxToolkit:MaskedEditExtender ID="txtQuan_MaskedEditExtender" runat="server"
Enabled="True" Mask="99999" TargetControlID="txtQuan" />
</ItemTemplate>
</asp:TemplateField>
Reason is that When Gridview Render row each Row contain textbox with deffferent autogenerated id and you are given a fixed name in TargetControlID Property in MaskEditExtender . so you need to use RowDataBound Event to Put MaskEditExtender in each textbox....
using AjaxControlToolkit; protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox txt = (TextBox)e.Row.FindControl("txtQuan");
MaskedEditExtender mxt = (MaskedEditExtender)e.Row.FindControl("MaskedEditExtender1");
mxt.TargetControlID = txt.ID;
}
}

Redirect to specific page when a gridview buttonfield is clicked

I have two fields in my gridview. One is a field with a date range and one of them is a buttonfield. I want to make it so that when a button is clicked, it will redirect them to a specific page with a formatted string similar to how it's done when a hyperlinkfield is clicked as such:
DataNavigateUrlFormatString="ProductInfo.aspx?ProductID={0}"
However, I don't want to use the other field's value(the date range). I want to use the primary key of the record which is "SundayOfWeek". The user wants a button rather than a hyperlink. Is there a way I can do the same thing with a button?
Try using this type of column instead of a ButtonField:
<Columns>
<asp:HyperLinkField
DataTextField="ProductName"
HeaderText="Product Name"
SortExpression="ProductName"
DataNavigateUrlFields="ProductID"
DataNavigateUrlFormatString="ProductInfo.aspx?ProductID={0}" />
</Columns>
Sure, one of many ways is this:
<columns>
<asp:templatefield headertext="Button">
<itemtemplate>
<asp:Button id="btnRedirect"
Text= "Click me"
CommandArgument='<%#Eval("SundayOfWeek")%>'
OnClick="DoRedirect"
runat="server"/>
</itemtemplate>
</asp:templatefield>
</columns>
protected void DoRedirect(object sender, EventArgs e)
{
Button theButton = sender as Button;
Response.Redirect("Newpage.aspx?ID="+theButton.CommandArgument);
}
If you don't want to use the date-range field, you should use another column as DataNavigateUrlField:
DataNavigateUrlField="KeyField"
DataTextField="DateField"
DataNavigateUrlFormatString="ProductInfo.aspx?ProductID={0}"
If you don't want to use a HyperLinkColumn, you should use a TemplateField and add a Button:
<asp:TemplateColumn>
<input type="button" onclick="location.href='ProductInfo.aspx?ProductID=<%#Eval("KeyField")%>'" value='<%#Eval("DateField")%>'' />
</asp:TemplateColumn>

How do I find the Client ID of control within an ASP.NET GridView?

I have a asp:GridView which contains a asp:TextBox within a TemplateField. I would like to obtain it's ID for use in javascript. Something like this:
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="textDateSent" runat="server" />
<input type="button" value='Today'
onclick="setToday('<%# textDateSent.ClientID %>');" />
</ItemTemplate>
</asp:TemplateField>
But when I compile, I get an error:
The name 'textDateSent' does not exist in the current context
Anybody know how to get the client ID of this TextBox?
Try this:
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="textDateSent" runat="server">
</asp:TextBox>
<input type="button" value='Today' onclick="setToday('<%# ((GridViewRow)Container).FindControl("textDateSent").ClientID %>');" />
</ItemTemplate>
</asp:TemplateField>
Maybe you don't want to do it where you need the ClientID. Check out this post here where the controls in a row are referenced in a generic way.
Change <%# textDateSent.ClientID %> to <%= textDateSent.ClientID %>.
Argh, you may need to use the OnDataBinding event of the grid view. Then put a literal control in your javascript. Then you can get the clientID of the text box and feed that into your literal control.
protected void GridViewName_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Create an instance of the datarow
DataRowView rowData = (DataRowView)e.Row.DataItem;
//locate your text box
//locate your literal control
//insert the clientID of the textbox into the literal control
}
}
Look here for a great detailed tutorial on working within this context.
You can get client id like this:
protected void Gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string strClientID = ((TextBox)e.Row.FindControl("txtName")).ClientID;
}
}
This will give unique client ID for each textbox in all rows.
I just do this...
var tbl = document.getElementById('<%=GridView.ClientID%>');
var checkBox = tbl.rows[i].cells[11].getElementsByTagName("input")[0].id;
the cell should always be the same and it gets rendered into an input. You may have to change the number at the end if you have more then one input in that cell. This will give you the new clientid/id of the input object (checkbox or whatever)
This is what I did. In the aspx page I just passed the entire object to the javascript function, so I didn't even meed to client id. In my case the object was a drop down list in the EditItemTemplate of the GridView. I added an html onchange(this) event in the aspx code.
<asp:DropDownList ID="custReqRegionsDDL" runat="server" onchange='custReqRegionsDDLOnChange(this)'>
</asp:DropDownList>
here is my javascript
function custReqRegionsDDLOnChange(myDDL)
{
alert('selected text=' + myDDL.options[myDDL.selectedIndex].text);
}

Accessing TextArea Inside GridView with JQuery BlockUI

This is driving me nuts!
I am trying to access a TextArea inside the GridView control. The TextArea is popped up when a button on a gridview is clicked. For some reason the textarea.value always contains " ".
<asp:GridView ID="gvCategories" runat="server" AutoGenerateColumns="false"
onrowcommand="gvCategories_RowCommand">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<input type="button" value="add comment" onclick="showCommentBox()" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<div id="commentBox" style="display:none">
<input type="button" value="move comment input box" onclick="moveComment()" />
<textarea id="txtComment" rows="10" cols="30">
</textarea>
</div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
function moveComment() {
alert(document.getElementById("txtComment").value);
}
I have added this code on the server side but the TextBox always returns " "
protected void gvCategories_RowCommand(object sender, GridViewCommandEventArgs e)
{
var row = (GridViewRow) (e.CommandSource as LinkButton).NamingContainer;
var description = (row.FindControl("txtDescription") as TextBox).Text;
lblComment.Text = description;
}
#Azam - This is related to your other post which I answered. The gridview is generating the commentBox DIV along with all its child elements multiple times with the same set of IDs.
I ran a test on this and found that each call to document.getElementById("txtComment") returns the next matching element in the DOM with that Id until it cycles through the entire collection of matching elements, going back to the first one and then it does it all over again.
This is the reason why you get a blank when trying to access the value of the textarea or textbox for that matter.
You need to modify your call to showComment() so that it stores a reference to the element in the given row, then when you call moveComment(), it will work on the same element and not just the next element in the DOM with the same ID.
try textarea.innerHTML
looking at your code that would be:
alert(document.getElementById("txtComment").innerHTML);

Use data in repeater when Checkbox is check in ASP.net

I have a repeater for showing my data . this repeater showing 2 field that one of feild is checkBox Control and other is a lable.
NOW , how can I understand text of lable when the checkBox is Checked?
I want to see text of lable in evry row that the CheckBoxes is checksd.
how do I do?
I use LINQtoSQL for get and set data from database
On postback, you need to loop through every row of your repeater, and grab out the checkbox control. Then you can access it's .Checked and .Text properties. If it's .Checked, then add it to a list or array. I can elaborate if needed..
Page...
<asp:CheckBox ID="chkBoxID" runat="server" OnCommand="doSomething_Checked" CommandArgument="<%# Some Binding Information%>"
CommandName="NameForArgument">
</asp:CheckBox>
Code Behind...
protected void doSomething_Checked(object sender, CommandEventArgs e) {
CheckBox ctrl = (CheckBox)sender;
RepeaterItem rpItem = ctrl.NamingContainer as RepeaterItem;
if (rpItem != null) {
CheckBox chkBox = (LinkButton)rpItem.FindControl("chkBoxID");
chkBox.DoSomethingHere...
}
}
<asp:Repeater ID="rptX" runat="server">
<ItemTemplate>
<asp:Label ID="lblX" runat="server" Visible='<%# Eval("IsChecked") %>' />
<asp:CheckBox ID="chkX" runat="server" Checked='<%# Eval("IsChecked") %>' />
</ItemTemplate>
</asp:Repeater>
And code behind when you assign your data
rptX.DataSource = SomeIEnumerableFromLinq; // which has a bool field called IsChecked
rptX.DataBind();

Resources