I have gridview putting data from one table from database using stored procedure.
On click of Edit Button, post back is happen which refreshing the data on gridview and wrong row is shown in edir mode.
I have found the reason is that on click on EDIT link javascript:__doPostBack('GridView','Edit$0') is displayed at the status bar, which is a problem here. Edit$0 means the absolute value of the row and when postback get the data from data base wrong row is shown in edit mode...
I think the solution will be put the edit mode based on not row number but some unique value of the selected row for edit.
Please help if any one has answer to it.
If you're using a gridview I suggest using the native OnRowEditing="GridViewEditEventHandler"
and then in the code behind
protected void TaskGridView_RowEditing(object sender, GridViewEditEventArgs e)
{
//Set the edit index.
TaskGridView.EditIndex = e.NewEditIndex;
//Bind data to the GridView control.
BindData();
}
see http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowediting(v=vs.110).aspx
However, if for some reason you are creating an OnClick function when you press the edit button and then changing the Itemtemplate to show/hide values I would suggest doing
GridViewRow gvr = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
int RowIndex = gvr.RowIndex;
from there you can do the .findcontrol to find the specific item you want ex:
TextBox txtbox = (TextBox)TaskGridView.Rows[RowIndex].FindControl("textBoxID");
Hope this helps
Related
I am new to asp.net website developer.
In my website I use GridView Control.In the GridView row i place some controls like
TextBox,DropDownList.
I can display the values in GridView.
But my requirement is ,Get the values from TextBox and DropdownList Which are existed in GridView.
Please help me to go forward..
thank you,
bye..
You need to access them by row. This code project article explains it in detail.
TextBox tb = (TextBox)gridview1.Rows[0].FindControl("idOfTextBox");
It above statement will find control in the first row of grid which has id idOfTextBox and type is textbox.
You can directly get any value of any control by casting directly, without using an extra textbox or dropdown variable.
Example:
string val = ((TextBox)gridview1.Rows[0].FindControl("TextBox_ID")).Text;
Hope it helps :)
Subscribe the RowDataBound event(this will be fired on each row available in gridview) of gridview and access like stated below,
protected void grdBillingdata_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//these controls are available inside gridview
HiddenField hdnNagetiveAmt = (HiddenField)e.Row.FindControl("hdnNagetiveAmt");
DropDownList ddlFeeType = (DropDownList)e.Row.FindControl("ddlFeeType");
TextBox txtFeeDesc = (TextBox)e.Row.FindControl("txtFeeDesc");
Button btnUpdate = (Button)e.Row.FindControl("btnUpdate");
}
}
not only text box we can get values from all the controls that used inside gridview that placed by using itemTemplate or simply a Bound field .
you need to loop for each row to get the values
foreach (GridViewRow row in grd_popup_details.Rows)
{
//get control values
}
Refference link
I have a boundfield in a gridview that programatically receives a hyperlink for the content in it. It gets a new dataset after a drop down list index change. One of the columns of the dataset will apply links to fields with data and skip those without, you can see my logic for applying links below:
if (e.Row.DataItem != null && int.TryParse(e.Row.Cells[4].Text, out incidents))
{
HyperLink incidentsLink = new HyperLink();
incidentsLink.ForeColor = System.Drawing.Color.Blue;
incidentsLink.NavigateUrl = "~/somesite.aspx?no=" + stnNum + "&dt=" + date;
incidentsLink.Text = e.Row.Cells[4].Text;
e.Row.Cells[4].Controls.Add(incidentsLink);
}
This is applied OnRowDataBound for the gridview. Then I have another gridview that is wired to another drop down list. When either drop down list changes index it grabs a new dataset for the related gridview and fires a ajax update using an update panel.
What happens is when the second gridview updates it erases the links in the first gridview. It doesn't erase the text, that stays, but the text is no longer anchored to a link. All links made this way and put into gridviews lose their link properties, however fields created using asp:HyperLinkField are left unchanged. The problem is that stnNum and date are not a part of the dataset that is returned for the first gridview, so I have to add them as a link after the gridview is already built.
The only solution I can think of is to refire the function that adds links to the gridview each time the second gridview updates. Any other solutions would be helpful, or an explanation as to why my link is being erased would be great.
Try binding the GridView/DropDowns only if there is not any POSTBACK. I believe POST Back event is letting your gridview & Controls to loose their data somehow.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Bind your Grids & Dropdowns here on page load
}
}
I have a gridview which has rows of 'tickets'. A user has the ability to 'claim' a ticket, this is all set up and works. What i need is to be able to change/hide the 'claim' button and replace it with a 'release' or 'open' command button. Can i achieve this at a rowdatabound level? What i do not want is to query the db everytime to see if the ticket is claimed.
You could cast the button from its Cell, then change its CommandName, and CommandArgs if needed, along with its text; eg use the same actual button for many purposes.
Im asssuming there is some status field that dictates what you can do with a record? Therefore on RowDataBound get this via a datakey, and adjust the button to suit.
Then on its click, check the command name and execute the relevant function / code block?
Edit - like this:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
Button btn = e.Row.Cells[YourButtonsColumIndex].FindControl("btnYourButtonsID") as Button;
btn.CommandName = "Release";
//Or
((Button)e.Row.Cells[YourButtonsColumIndex].FindControl("btnYourButtonsID")).CommandName = "Release";
}
Bearing in mind hidden rows still count in the zero-index column list.
1) I noticed that if we don’t bind GridView to object data source control, then when user puts GridView into edit mode, we have to handle GridView.RowEditing event (else we get an exception ) and in this event put GridView’s row into editing mode. Is there a reason why GridView doesn’t automatically put a row into edit mode?
2) When we manually bind GridView to one of DataSet’s tables and user puts a row into edit mode, row’s columns will replace fields with text boxes. But for some reason these text boxes don’t display current field values, but instead they don’t display any text at all. What am I doing wrong?
3) I’ve also handled gridView.RowUpdated event, so I could put row back into non-edit mode, but to no effect. I even tried by pressing Edit button of some other row, but row still wouldn’t go out of edit mode. Any ideas what I’m doing wrong?
protected void gvwEmployees_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
e.KeepInEditMode = false;
}
Thanx
When not using a DataSource control with a GridView or other data-bound control which hide the complexity of the manual data-binding you must manually handle RowEditing, RowUpdating, and RowDeleting etc. With the built in data model and automatic binding the GridView handles these events for you.
You haven't posted your RowEditing code, but i suspect that you are not setting the GridViews EditIndex to the NewEditIndex and are not rebinding, this is probably why you are not seeing current data.
protected void gvwEmployees_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView.EditIndex = e.NewEditINdex;
BindData();
}
The same is true for your RowUpdating event. You will have to manually update your data, then set the EditIndex to -1, this will put your GridView back into ReadOnly mode. Keep in mind that e.OldValues, e.NewValues and e.Keys properties of the GridViewUpdateEventArgs are not populated when binding manually. This mean you'll have to take care of the update yourself by using e.RowIndex which is the index of the edited row.
protected void gvwEmployees_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridView.EditIndex = -1;
BindData();
}
I have a Gridview which is binded to a datatable that I created. The data in the table is ever changing.
I added a buttonfield to the Gridview. When clicked I want to send a specific columns value in that row through a link or sent through a function.
How do you go about doing this?
I found out how to do it. You set the commandName property to whatever you want to call it inside the properties.
Then if you double click on the button after creating it in design view, it should pop up a function in the code behind page. You can then access the row by doing the following.
protected void gvUsers_RowCommand(object sender, GridViewCommandEventArgs e)
{
int rowNum = int.Parse(e.CommandArgument.ToString());
}
from there you can use the row number to access specific column data.
Here's a decent example from MSDN: http://msdn.microsoft.com/en-us/library/bb907626.aspx