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.
Related
I got i strange situations.
This is a part of the ascx code of my gridview
<asp:gridview ID="grdBooks" class="grdBooks" runat="server" width="100%" DataKeyNames="RecId,RefAuthorsRecId,RefBooksTypeRecId" autogeneratecolumns="false" onDataBound="grdBooks_dataBound" onPreRender="grdBooks_preRender" onrowediting="grdBooks_RowEdit" onrowupdating="grbBooks_onRowUpdating" onrowupdated="grdBooks_onUpdated" autogenerateeditbutton="true" onrowcancelingedit="grdBooks_onCancelingEdit">
<Columns>
<asp:TemplateField HeaderText="Title">
<EditItemTemplate>
<asp:TextBox ID="txtgrdTitle" runat="server" width="200px"></asp:TextBox>
<asp:Label ID="lblgrdTitle2" runat="server" visible="false" Text='<%# Eval("Title") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblgrdTitle" runat="server" Text='<%# Bind("Title") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>.....
This is a part of the "onRowEdit" and "onRowUpdating"
protected void grdBooks_RowEdit(object sender, GridViewEditEventArgs e)
{
grdBooks.EditIndex = e.NewEditIndex;
grdBooks.DataSource = book;
grdBooks.DataBind();
TextBox txtTitle = grdBooks.Rows[e.NewEditIndex].FindControl("txtgrdTitle") as TextBox;
Label lblTitle = grdBooks.Rows[e.NewEditIndex].FindControl("lblgrdTitle") as Label;
txtTitle.Text = lblTitle.Text;
}
protected void grbBooks_onRowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox txtTitle = grdBooks.Rows[e.RowIndex].FindControl("txtgrdTitle") as TextBox;
....
}
The problem is:
the rowEdit works fine, change all the row of the grid in editable textboxes. The problem is that i write something in the text boxes and when i press "update" and go into the "onRowUpdate" method i haven't the new text(that i have inserted) in the textbox object that i have initialize. I got the old one.
(as u see now i use a not visible label in the editTemplate that carry the value of the original Title. Before i had Eval("title") on the textbox text but i thought that this was the probl so i try this other way)
As your datasource is book.Get the currentrow of the datatable and update values in that row.Then bind the grid again.
GridViewRow gridEditRow = this.grdBooks.Rows[e.RowIndex];
DataRow CurentRow = book.Rows[gridEditRow.DataItemIndex];
CurentRow["Title"] = (gridEditRow.FindControl("txtgrdTitle") as TextBox).Text;
This is a simple gridView I m using in aspx web page
<asp:GridView ID="Order" runat="server" AutoGenerateColumns="False" ShowFooter="True" GridLines="none"
ItemType="MyProject.Models.TempList" SelectMethod="GetList" >
<Columns>
<asp:BoundField DataField="ItemID" HeaderText="ID" SortExpression="ItemID" />
<asp:TemplateField HeaderText="ItemName">
<ItemTemplate>
<asp:Label runat="server" ID="ItemName" Width="40" Visible="true" text="<%#: Item.Item.ItemName %>"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Price(each)">
<ItemTemplate>
<%#: String.Format("{0:c}", Convert.ToDouble(Item.Item.UnitPrice))%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<div style="float:left">
<asp:Button ID="DeleteBtn" runat="server" Text="Delete" OnClick="DeleteBtn_Click"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I have 2+ items in a list<> which is populating the gridView, how can I tell (in the codeBehind.cs) that which Row the "DeleteBtn" was clicked on?
I was using a for loop to iterate every item in gridView using Rows[i] and used a check box to know which item wants to be deleted using a Update button.
But I want to do it directly on a custom created deletebutton.
Thanks in advance, I know I'm missing something silly.
The best way to do that is using CommandName and CommandArgument in your button declaration like that
<asp:Button ID="AddButton" runat="server" CommandName="AddToCart"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" Text="Add to Cart" />
you can pass any value in the case we pass the rowIndex, you can get a propertie from your object like that CommandArgument="<%# Eval("id") %>" after that you will handle the onRowCommand method
protected void GridView1_RowCommand(object sender,GridViewCommandEventArgs e)
{
if (e.CommandName == "AddToCart")
{
// Retrieve the row index stored in the
// CommandArgument property.
int index = Convert.ToInt32(e.CommandArgument);
// Retrieve the row that contains the button
// from the Rows collection.
GridViewRow row = GridView1.Rows[index];
// Add code here to add the item to the shopping cart.
}
}
hope it helps :)
Use the button's CommandArgument property; assign Container.DataItemIndex to it. Then use the OnRowCommand event of the gridview and grab the index.
Sample aspx:
<asp:Label runat="server" ID="lblMsg" />
<asp:GridView runat="server" id="gvSample" AutoGenerateColumns="false" OnRowCommand="PerformOperation">
<Columns>
<asp:BoundField DataField="RowValue"/>
<asp:TemplateField>
<ItemTemplate>
<asp:Button Text="Delete" runat="server" CommandName="MyCustomCommand" CommandArgument="<%# Container.DataItemIndex %>" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
PopulateGrid();
}
}
private void PopulateGrid()
{
gvSample.DataSource = Enumerable.Range(0, 10).Select(i => new { RowValue = i });
gvSample.DataBind();
}
protected void PerformOperation(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "MyCustomCommand")
{
var rowIndex = int.Parse(e.CommandArgument);
lblMsg.Text = string.Format("Button on row index: {0} was clicked!", rowIndex);
}
}
Let the button field be given a CommandName, say for example a unique string myCommandName in the Edit columns dialog box. Don't worry about the CommandArgument. Then, in the Grid view Row command event, you can check which button (i.e. column) is clicked by tracing the command name like if e.commandname = "mycommandname", at the same time the CommandArgument will also be available as String and all we have to do is to converttoint32, something like intSelectedRow = convert.ToInt32(e.CommandArgument) which will give us the selected row's index.
I am trying to modify the content of a TextBox in the OnRowEditing event of the gridview but I cannot seem to find the TextBox control in code behind.
The field in aspx:
<asp:TemplateField HeaderText="AssignTo" SortExpression="AssignTo">
<EditItemTemplate>
<asp:TextBox ID="txtAssignTo" runat="server" Text='<%# Bind("AssignTo") %>' ClientIDMode="Static"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("AssignTo") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Trying to find it in code behind:
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridViewRow grow = GridView1.Rows[e.NewEditIndex];
TextBox txt = grow.FindControl("txtAssignTo") as TextBox;
}
The txt variable is always null. Not sure what I am missing here.
You have to bind the gridview after you set the editindex (call its DataBind() method)
I have a problem with my gridview. I have searched alot for the solution but can't find any answers. I think I have located the problem to be that the gridview is no longer bound when I press the update button - which results in null values. I thought it was enough to bind at RowEditing. Where else can I bind my gridview?
See the markup below:
<asp:GridView ID="ProductGridView" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" DataKeyNames="Id" OnRowEditing="ProductGridView_RowEditing"
OnRowCancelingEdit="ProductGridView_RowCancelingEdit" OnRowUpdating="ProductGridView_RowUpdating"
OnRowDeleting="ProductGridView_RowDeleting" OnDataBound="ProductGridView_DataBound" OnRowDataBound="ProductGridView_RowDataBound">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" CausesValidation="false" />
<asp:TemplateField HeaderText="Name" SortExpression="Name">
<EditItemTemplate>
<asp:TextBox ID="txtName" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity" SortExpression="Quantity">
<EditItemTemplate>
<asp:TextBox ID="txtQuantity" runat="server" Text='<%# Bind("Quantity") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblQuantity" runat="server" Text='<%# Eval("Quantity") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Family" SortExpression="Family.Name">
<EditItemTemplate>
<asp:DropDownList ID="ddlFamily" runat="server" OnInit="ddlFamily_Init">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblFamily" runat="server" Text='<%# Eval("Family.Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And the code-behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindGridView(_productRepo.GetAll());
}
}
private void BindGridView(object source)
{
ProductGridView.DataSource = source;
ProductGridView.DataBind();
}
protected void ProductGridView_RowEditing(object sender, GridViewEditEventArgs e)
{
ProductGridView.EditIndex = e.NewEditIndex;
BindGridView(_productRepo.GetAll()); // GetAll returns an IEnumerable.
rowCount = ProductGridView.Rows.Count; // Count is 6 here, which is correct.
}
protected void ProductGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
rowCount = ProductGridView.Rows.Count; // Count is 0 here.
//BindGridView(_productRepo.GetAll()); // Tried to rebind which works but getting the old values obviously.
//rowCount = ProductGridView.Rows.Count; // Count is 6 here.
// Get the controls - all is null. Works ok when I use BindGridView above.
TextBox txtName = FindChildControl<TextBox>(this.Page, "txtName");
TextBox txtQuantity = FindChildControl<TextBox>(this.Page, "txtQuantity");
DropDownList ddlFamily = FindChildControl<DropDownList>(this.Page, "ddlFamily");
// More code to populate a new product and bind the gridview again etc.
}
I also have a RowDataBound method. Can this contribute to the problem?
protected void ProductGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit)
{
DropDownList ddl = (DropDownList)e.Row.FindControl("ddlFamily");
ddl.DataSource = _familyRepo.GetAll().Select(f => f.Name);
ddl.DataBind();
Product product = _productRepo.FindSingle(p => p.Id == (int)ProductGridView.DataKeys[e.Row.RowIndex].Value);
ddl.SelectedIndex = (int)product.FamilyID - 1;
}
}
If I understand you correctly, you are saying your form controls within your datagrid disappear or reset to their initial state after postback.
The reason this is happening is because you are binding your grid in the Page_Load method, which comes too late in the page lifecycle to restore the control values. Your grid doesn't load until AFTER the LoadViewstate and LoadPostbackData events are fired and therefore, your grid's controls are loaded with their original state every time a postback is made.
I'd guess you're familiar with the asp.net lifecycle, but if not here is an article: http://msdn.microsoft.com/en-us/library/ms972976.aspx. I've dealt with this issue many times, and it took me awhile to fully understand what was going on here.
One solution to the problem is to load the grid in the overridden OnInit method, which occurs before the control data is restored. Something like this should work:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
BindGridView(_productRepo.GetAll());
}
I usually do data binding this way.... try this function and call it in your page load and other functions where you need it.
protected void bind()
{
con.Open();
SqlCommand cmd = new SqlCommand("Your Query", con);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
da.Fill(ds);
gvCourse.DataSource = ds;
gvCourse.DataBind();
con.Close();
}
Or you can simply replace the !Page.IsPostBack with !IsPostBack
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridView(_productRepo.GetAll());
}
}
i have a gridview like this :
<asp:GridView ID="wbsdataGV1" runat="server" Width="790px" AutoGenerateColumns="False"
OnSelectedIndexChanged="wbsdataGV1_SelectionChanged">
<Columns>
<asp:TemplateField HeaderText="WBS Code">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" Text='<%# Eval("WBSCode") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<asp:TextBox ID="TextBox7" runat="server" Text='<%# Eval("Description") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Territory Code">
<ItemTemplate>
<asp:TextBox ID="TextBox8" runat="server" Text='<%# Eval("TerritoryCode") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Amount">
<ItemTemplate>
<asp:TextBox ID="TextBox9" runat="server" Text='<%# Eval("AmountReleased") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
when i am clicking on the linkbutton i want that data and i will transfer that to another aspx from .. through
protected void wbsdataGV1_SelectionChanged(object sender, EventArgs e)
{
//Some code ;
}
now my question is how i can get that data by clicking the linkbutton on gridview ? i can transfer the data by querystring or session variable or hidden text field ... but my concern is that how can i get the exact clicked data...
any help?
Define the DataKeys attribute to the be primary key of your data (here I assume you have a row identifier called "ID".
<asp:GridView ... DataKeys="ID">
... existing markup
</asp:GridView>
In wbsdataGV1_SelectionChanged, get back the ID of the row the user clicked like this:
int rowID = (int)this.wbsdataGV1.SelectedDataKey.Value;
Then redirect to another page like this, passing just the ID:
Response.Redirect("anotherpage.aspx?id=" + rowID);
Then in your other page:
protected void Page_Load(object s, EventArgs e)
{
if (!Page.IsPostback)
{
int rowID = int.Parse(Request.QueryString["id"]);
// do something with the ID of the row, like go and look
// up JUST that row again
}
}
Bonus - to make the whole row clickable, avoiding the need for a CommandButton, do this:
<script language="javascript" type="text/javascript">
var oldColour = null;
function rowMouseover(o) {
o.style.cursor = 'pointer';
oldColour = o.style.backgroundColor;
o.style.backgroundColor = '#dddddd';
}
function rowMouseout(o) {
o.style.backgroundColor = oldColour;
}
</script>
<asp:GridView ... OnRowCreated="grid_RowCreated" />
protected void grid_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onclick"] =
Page.ClientScript.GetPostBackClientHyperlink(
this.grid,
"Select$" + e.Row.RowIndex);
e.Row.Attributes["onmouseover"] = "rowMouseover(this);";
e.Row.Attributes["onmouseout"] = "rowMouseout(this);";
}
}
protected override void Render(HtmlTextWriter writer)
{
for (int i = 0; i < grid.Rows.Count; i++)
Page.ClientScript.RegisterForEventValidation(grid.UniqueID, "Select$" + i);
base.Render(writer);
}
You could pass the ID via the Session object, but you'd get odd behaviour with more than one browser window doing the same operation at the same time (depending on the browser). This would be hard for a user to tamper with.
You could pass the ID via Request.Form too, that would hide it from trivial user fiddling but doesn't add any true security.
Protip: don't just dump massive objects into the Session variable. It's lazy, error prone, and doesn't scale.