I have a Gridview control and I have placed a RadioButton in the itemtemplate. When I click a button I'm trying to get the checked property of the radio button. But When I click the checked property is always returning false. Please look into the below code and let me know where I'm making mistake.
aspx Code
<asp:GridView ID="gvDepartments" runat="server" AutoGenerateColumns="False" GridLines="None"AllowPaging="true" CssClass="mGrid" PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:RadioButton ID="rdbtn" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Label ID="lbl" runat="server"></asp:Label>
<asp:Button ID="btn" runat="server" Text="Save" OnClick="btn_Click" CssClass="button small"/>
Code Behind on button click
foreach (GridViewRow row in gvDepartments.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
RadioButton rd = (RadioButton)row.FindControl("rdbtn");
lbl.Text += rd.Checked.ToString();// +rd1.Checked.ToString();
}
}
Result will be False always in the label always. I'm using Ajaxcontroltoolkit in the application, Above controls are present in the update panel and I even tried to placing button and event in the triggers but the result is same. Please help.
Regards,
Nuthan A R
When do you assign and load the DataSource of the GridView? Note that you should do that only if(!IsPostBack) and not on every postback. So use the Page.IsPostBack property.
So assuming that you're using Page_Load for this:
protected void Page_Load(Object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
DataBindGridView(); // method that assigns DataSource and calls DataBind
}
}
Related
I have a gridview on a .NET forms application, and on postback, I am not seeing the values entered in the textbox within a gridview.
ASPX:
<asp:GridView ID="gvItems" runat="server" AutoGenerateColumns="false" ShowHeader="false" DataKeyNames="ItemId" EnableViewState="true">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="txtItem" runat="server" Text="0" EnableViewState="true" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ItemId" />
</Columns>
</asp:GridView>
<asp:Button runat="server" ID="btn" Text="Submit" OnClick="btn_OnClick" OnClientClick="javascript:return someClientStuff();" />
Code Behind:
protected void btn_OnClick(object sender, System.EventArgs e)
{
foreach(GridViewRow row in gvItems.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
var itemId = Convert.ToInt32(gvItems.DataKeys[row.RowIndex].Values[0]);
var itemValue = ((row.Cells[0].FindControl("txtItem") as TextBox).Text;
}
}
I am seeing itemId populated for each row, but itemValue is always empty string.
Been a while since I worked on a forms application, any help is appreciated!
I will assume that the DataBind of the GridView is not in if(!IsPostBack). Add this to the Page_Load
if(!IsPostBack)
{
gvItems.DataSource = soruceOftheGrid
gvItems.DataBind();
}
On the button click, try to store the values in the view state and within page load event, try to assign the viewstate back to the grid view as for each postback, page load event is called. Its good to assign the values back to grid view within page load, this will help you to retain the values you have entered.
I have a gridview in my ASPX page. This GridView is inside a UpdatePanel which causes partial page postbacks.
I have a button as the last column for each row in the GridView. How do I open temp.aspx page with ID value passed to it onClick of each button. I have a ID column in the gridView too.
Please help
You can get the DataSource for each row OnRowDataBound, then find the control you want in that row and apply your OnClick JavaScript:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Example"
<ItemTemplate>
<asp:label ID="YourSpanToClick" runat="server" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label yourSpanToClick= (Label)e.Row.FindControl("YourSpanToClick");
YourDataSrcRowObject rowObject= DataBinder.Eval(e.Row.DataItem) as YourDataSrcRowObject ;
string js = String.Format("window.open('{0}');", "temp.aspx?Id=" + rowObject.Id)
yourSpanToClick.Attributes.Add("onclick",js);
}
}
You can't open an new window from OnClick event. Try the OnClientClick inside the RowDataBound event of that gridview.
Private Sub DataGridView1_OnRowDataBound(Sender as Object, e as DataGridView.EventArgs)
Dim btn as Button=e.Row.FindControl("btnName")
btn.OnClientClick="javascript:window.open(""temp.aspx?id=22"")
End Sub
Something like this.
Hi I have a grid view with a textbox in each row that im trying to get the value of in the RowCommand event. The below code works fine for all rows expect the first one. The textbox.text value for the first row in always empty.
<asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_OnRowCommand" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
Title <%# Eval("Title")%>
<asp:TextBox ID="TextBoxAddPost" runat="server"></asp:TextBox>
<asp:LinkButton ID="LinkButtonAddPost" CommandName="AddPost" CommandArgument='<%# Eval("postId") %>' runat="server">Add Post</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code-behind:
protected void Page_Load(object sender, EventArgs e)
{
if(IsPostBack)
bindGridView();
}
protected void GridView1_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "AddPost")
{
GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
TextBox textBox = (TextBox)row.FindControl("TextBoxAddPost");
//Empty for first row but works for all others
Debug.WriteLine("row: " + row.RowIndex + ", textBox:" + textBox.Text.Trim());
GridView1.DataBind();
}
}
The above code has been simplified for illustration purposes. Each row actually contains a child gridview, hence why in need the text box in each row. I fear that the binding in the page_load is overwriting the value of the text box, however, without the page_load binding, the rowCommand Event is not fired.
I find i a bit strange the it works fine for all rows except the first.
For getting data from textbox, You have to set text property first by putting below code.
<asp:TextBox ID="TextBoxAddPost" runat="server" Text='<%# Eval("Title") %>'></asp:TextBox>
It will definitely give value from textbox.
Either way, You can also set datakeynames property of gridview.Click here for datakeynames
I tried this and it works fine,GridView1_OnRowCommand fires by clicking on LinkButtonAddPost:
<asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="TextBoxAddPost" runat="server" Text='<%# Eval("ID") %>'></asp:TextBox>
<asp:LinkButton ID="LinkButtonAddPost" CommandName="AddPost" CommandArgument='<%# Eval("ID") %>' runat="server">Add Post</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and change your page_load event like this:
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataSource = Data.RequestPaymentDB.GetRequestPaymentByRequestID(9208060001);
GridView1.DataBind();
}
compare your code with mine.
Hi I need help implementing a logic using Gridview control in c#.
I have a gridview and it has many rows. Each row has a Button to click for the user. On each button click i am updating the selected record in the database. Now once row is updated, I need to hide that button to prevent reaction just for that particular row.
1. If i use this
<asp:CommandField ShowEditButton="True" EditText="select" />
, I can't make this hide.
2. If I use this
<asp:TemplateField HeaderText="Your Action">
<ItemTemplate>
<asp:Button
ID="btnAccept"
runat="server"
Text="Accept"
OnClientClick="return confirm('Are you sure you want to Accept this offer?');"
onclick="btnAccept_Click" />
</ItemTemplate>
</asp:TemplateField>
, I can't get the selected row index.
I hope i have cleared what i wanna ask. Thanks in advance.
Use Button control's CommandArgument property specify the row user clicked :
<asp:TemplateField HeaderText="Your Action">
<ItemTemplate>
<asp:Button
ID="btnAccept"
runat="server"
Text="Accept"
OnClientClick="return confirm('Are you sure you want to Accept this offer?');"
CommandName="Accept"
CommandArgument='<%# Eval("RowId") %>'
onclick="btnAccept_Click" />
</ItemTemplate>
</asp:TemplateField>
At code behind :
void btnAccept_Click(Object sender, CommandEventArgs e)
{
if (e.CommandName == "Accept")
{
string rowId = e.CommandArgument;
}
}
Continuing on Canavars solution for 1):
void btnAccept_Click(Object sender, CommandEventArgs e)
{
if (e.CommandName == "Accept")
{
string rowId = e.CommandArgument;
((Button)sender).Visible = false;
}
}
I have 3 gridview control in my page. each grid itself i am using check box as a TemplateField.when i am selecting the first grid check box. it displays some records.In this time when i am press the F5(Referesh) button.All the events in the Page not fired.
check box in the Grid:
<asp:GridView ID="gvSelectQuest" runat="server" AutoGenerateColumns="False"
AllowSorting="True" onrowdatabound="gvQuestion_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate >
<asp:CheckBox ID="chkSelectGrp" AutoPostBack ="true" OnCheckedChanged ="CheckChanged" runat="server"/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="_quest_AdminID" HeaderText="AdminID" />
</Columns>
</asp:GridView>
Check box Event:
public void CheckChanged(object sender, EventArgs e)
{
CheckBox chk = (CheckBox)(sender);
foreach (GridViewRow rowItem in gvQuesGroup.Rows)
{
}
}
Please any one Help me.
Use this for the checkbox in the ItemTemplate to fire the event, dont press F5 to trigger
<asp:CheckBox ID="chkQuest" runat="server" OnCheckedChanged="CheckChanged" AutoPostBack="True" />