I am trying to update a user in a different page with textboxes. But what I'm trying to do is select the user based on a Gridview.
So on the first page You see a gridview with a user list and checkboxes. You select a user with a check box and then hit the Edit User button. Then it goes to a new page which allows you to edit the user in textboxes but populates the data because of the ID or another unique column. How would I do this??
Here is the code I have so far:
protected void Button2_Click(object sender, EventArgs e)
{
foreach (GridViewRow gvrow in GridView1.Rows)
{
CheckBox CheckBox1 = (CheckBox)gvrow.FindControl("chkSelect");
if (CheckBox1.Checked)
{
//Go to new page to edit user
}
else
{
//Do nothing if not checked
}
}
}
I assume that your checkbox is inside a <asp:TemplateField>.
So, in a <asp:TemplateField> you could add a <asp:HiddenField /> to store the ID of the user. Then you can get the ID by getting the value of the <asp:HiddenField />
ASPX markup:
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
<asp:HiddenField ID="hfdUserID" runat="server" Value='<%#Eval("id") %>' />
</ItemTemplate>
</asp:TemplateField>
Code behind:
protected void Button2_Click(object sender, EventArgs e)
{
int userid = 0;
foreach (GridViewRow gvrow in GridView1.Rows)
{
CheckBox CheckBox1 = (CheckBox)gvrow.FindControl("chkSelect");
if (CheckBox1.Checked)
{
userid = Convert.ToInt32(((HiddenField)gvrow.FindControl("hfdUserID")).Value);
Response.Redirect("useredit.aspx?id=" + userid.ToString());
}
}
}
Edit: To get the user ID to the new page you can use the following and then use it accordingly
int userid = Convert.ToInt32(Request.QueryString["id"]);
Related
Hi I would like to know how to get the text field and index from a hyperlink in a gridview that has been clicked. Basically, the user would click on the hyperlink in the gridview and when the user has been navigated to the link, the text field and index of the link would be stored into an arraylist. Does anyone have any idea how I can go about doing this?
I have came up with this "pseudo code" for the onrowdatabound event handler in gridview:
ArrayList linksClicked = new ArrayList();
if (e.Row.RowType == DataControlRowType.DataRow)
{
HyperLink hl = (HyperLink)e.Row.FindControl("links");
if (hl != null)
{
linksClicked.Add(h1.ToString());
}
}
You should use ItemTemplate with LinkButton. In this button you can keep index or id like CommandArgument, also you easily catch event onClick and add index to your array. Use this sample.
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="hyperLinkButton" Text="link" PostBackUrl="youruri.com" runat="server"
CommandArgument="<%# Eval("SomeFieldYouNeedArguementFrom") %>" OnClick="hyperLinkButton_Click" >
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
protected void hyperLinkButton_Click(object sender, EventArgs e)
{
LinkButton btn = (LinkButton)(sender);
string yourValue = btn.CommandArgument;
// do what you need here
}
I have a GridView in a ASP.NET web application, in which I have added image button in each row:
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="edit" runat="server" CommandArgument='<%# Bind("EmpID") %>' CommandName="edituser" ImageUrl="image/images.jpg"
ToolTip="Edit User Details"> </asp:ImageButton>
</ItemTemplate>
</asp:TemplateField>
Now how I can get the row data from gridview simply by clicking an edit image button in a row?
You have to change the CommandArgument with this one:
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>
Then:
protected void GridView1_RowCommand(object sender,
GridViewCommandEventArgs e)
{
if (e.CommandName == "edituser") /*if you need this
{
// Retrieve the row index stored in the
// CommandArgument property.
int index = Convert.ToInt32(e.CommandArgument);
// Retrieve the row that contains the buttonfrom the Rows collection.
GridViewRow row = GridView1.Rows[index];
// Add code here now you have the specific row data
}
}
Bind all the columns in the label control respectively, and you can get value using findcontrol method at GridView1_SelectedIndexChanged event
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
Label _lblText = (Label)this.GridView1.SelectedRow.FindControl("UR_Desired_Label_ID");
this.TextBox3.Text = _lblText.Text ;
}
I have used grid view in my ASP.NET application.
<asp:GridView ID="grdView" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkbox" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Jurisdiction">
<ItemTemplate>
<asp:Label ID="lblJurisdiction" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="License Number">
<ItemTemplate>
<asp:TextBox ID="txtLicenseNumber" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
in cs file
protected void btnSave_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in grdView.Rows)
{
for (int i = 0; i < grdView.Columns.Count; i++)
{
String cellText = row.Cells[i].Text;
}
}
}
Note that the above grid will be populated by data. Now I need to get data from already populated gridview. The above code is not working. Also I need to retrieve from labels, textboxes, checkboxes values inside grid. Please help !!!
You can use FindControl method to retrieve the control's data:-
protected void btnSave_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in grdView.Rows)
{
CheckBox chkbox = row.FindControl("chkbox") as CheckBox;
Label lblJurisdiction = row.FindControl("lblJurisdiction") as Label;
..and so on
//Finally retrieve the data like your normal control
string labelText = lblJurisdiction.Text;
}
}
Use GridViewRow.FindControl method.
foreach (GridViewRow row in grdView.Rows)
{
// return you the check-box control from current row
var chkbox = row.FindControl("chkbox");
}
Check whether the row type is DataControlRowType.DataRow.
protected void btnSave_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in grdView.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i < grdView.Columns.Count; i++)
{
String cellText = row.Cells[i].Text;
}
}
}
}
To get the TextBox, CheckBox value from the grid use this,
string TextBoxvalue = ((TextBox)GridViewID.Rows[i].FindControl("TextBoxName")).Text;
string CheckBoxvalue = ((CheckBox)GridViewID.Rows[i].FindControl("CheckBoxName")).Text;
In your code .cs file, you are missing to check only for datarow. As it will check all 3 places:-
1. Header
2. Body
3. Footer
Might any one place, any exception can occur.
Please add one more if condtion just after the for loop, as below.
if (row.RowType == DataControlRowType.DataRow)
{
Hope this post will help's you :).
Use
cells[i].EditedFormatedValue
I have a Gridview with two buttons and I want to be able to run code when the user clicks on them. I have tried using Row_command and setting a CommandName on the buttons , but i am going round in circles! Help!
I cant seem to get the username name from the first cell in order to search for the user in the rowcommand:
protected void gridview_search_RowCommand(object sender,
GridViewCommandEventArgs e)
{
if (e.CommandName == "unlock_account")
{
string user = gridview_search.SelectedRow.Cells[0].ToString();
//run code when user is obtained
}
}
should be able to set the event-handlers for those buttons to whichever function you are trying to run.
dgvbutton1.clickevent = dosomething();
dgvbutton2.clickevent = dosomething();
something along those lines should work
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="AddButton" runat="server"
CommandName="AddToCart"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
Text="Add to Cart" />
</ItemTemplate>
</asp:TemplateField>
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.
}
}
My syntax was wrong for the command name part. I finally figured it out and then was able to get selected value from the gridview using DataKeys and was then able to
send this value to unlock user account.
protected void gridview_search_RowCommand(object sender,
GridViewCommandEventArgs e)
{
if (e.CommandName.CompareTo("unlock_account") == 0)
{
int user = (int)gridview_search.DataKeys[Convert.ToInt32(e.CommandArgument)].Value;
//unlock account method
userObj.unlockAccount(user);
}
}
My Button1 is inside a Panel, I want to access the rowindex so that I will hide that Imagebutton. But when I enter debug mode, the GridView1.SelectedIndex has a null value. pleaase help!
protected void Button1_Click1(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
if (row.RowIndex == Convert.ToInt32(GridView1.SelectedIndex))
{
ImageButton StopButton = (ImageButton)row.FindControl("stopImageButton");
ImageButton StartButton = (ImageButton)row.FindControl("startImageButton");
StopButton.Visible = true;
StartButton.Visible = false;
}
}
}
this.StopTimeNotesPanel_ModalPopupExtender.Hide();
}
You said that Button is inside a panel. To be able to handle GridView events effectively, use a button inside Gridview itself.
OR if you still want to use button in panel, then,
1.) First Add a select button inside Gridview. Select a row using the select button and
2.) click the button in Panel.
GridView.SelectedIndex is set only when you have selected a row of Grid view. Two ways are possible:
1.) Set AutoGenerateSelectButton property to true.
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSource"
autogenerateselectbutton="True"
runat="server">
2.) Add yourself a buttonField inside section of gridView as:
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSqlDataSource"
autogeneratecolumns="false"
runat="server">
<columns>
<asp:buttonfield buttontype="Button"
commandname="Select"
headertext="Select Customer"
text="Select"/>
<asp:boundfield datafield="CompanyName"
headertext="Company Name"/>
</columns>
</asp:gridview>
Now after a row is selected, two events of GridView will be fired:
selectedindexchanging & selectedindexchanged.
Only when required, do this step below to get SelectedRow in SelctedIndexChangedEvent
void CustomersGridView_SelectedIndexChanged(Object sender, EventArgs e)
{
// Get the currently selected row using the SelectedRow property.
GridViewRow row = CustomersGridView.SelectedRow;
MessageLabel.Text = "You selected " + row.Cells[2].Text; // just for Display
}
Now, inside your button click event , get the selected index:
protected void Button1_Click1(object sender, EventArgs e)
{
int i = CustomersGridView.SelectedIndex;
}
Code for for your foreach loop:
ImageButton StopButton = (ImageButton)row.FindControl("stopImageButton");
ImageButton StartButton = (ImageButton)row.FindControl("startImageButton");
StopButton.Visible = true;
StartButton.Visible = false;