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);
}
}
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 am building a web application in ASP.net and I have a little problem.
I have a LISTVIEW to display data from a data source, and in that listview I have included a BUTTON in every row to be visible if the result of the query in the Page_load is 0.
The Query works, but I don't know how to select the button in the query.
I have tried
ListView1.FindControl("hiddenButton").visible = false;
this is the buttons code
<asp:Button ID="hiddenButton" runat="server" CommandArgument ='<%# Eval("ProfileId") %>' Text="Add Friend" CssClass="btn btn-info pull-right" OnClick="addFriend_Click" Width="105px" allign="right"/>
But its not working.
You can do this in ItemDataBound event:-
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType==ListViewItemType.DataItem)
{
if (YourCondition)
{
Button hdn = (Button)e.Item.FindControl("hiddenButton");
hdn.Visible = false;
}
}
}
You need to associate this event handler in your mark-up(if not already done):-
<asp:ListView ID="ListView1" OnItemDataBound="ListView1_ItemDataBound">
</asp:ListView>
You can use ItemDataBound event To set Buttons visible to True/False
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
ListViewDataItem dataItem = (ListViewDataItem)e.Item;
if (e.Item.ItemType == ListViewItemType.DataItem)
{
Button hiddenButton=(Button) dataItem.FindControl("hiddenButton");
hiddenButton.Visible = false;
}
}
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"]);
I had placed div with condition.. on my execution,if condition is also displayed, i need to remove it..
**My New Code**
<asp:TemplateField HeaderStyle-Width="90px" ItemStyle-Width="0">
<ItemTemplate>
<div style="cursor: pointer; padding-top: 02px;" onclick="ShowfllDetails(<%#Eval("StudentID")%>);">
if(<%# (Eval("StatusName").Equals("Processed")) %>)
{
//should not show the upload button
}
else
{
<u>Upload </u> //show the upload button
}
</div>
<asp:Image ID="Image1" runat="server" ImageUrl='<%#(Eval("StatusName").Equals("Processed") ? "images/add_btn.png" : "")%>' />
</ItemTemplate>
I'm getting displayed the if condition i need not to display it..
Thank-You.
I have done such a functionality inside Gridview, I assume you are doing the same. In place of tag you can use linkbutton set commandArgument and Commandname properties. AFter that fire Gridview_Rowcommand event. when ever you will click on linkbutton this event will fire and you can set the status in database or somewhere in session that this link is clicked against student id
<asp:LinkButton ID="LinkButton1" runat="server" Text="Upload" CommandName="Upload"
CommandArgument='<%#Eval("StudentID")%>'></asp:LinkButton>
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Upload")
{
// get student id against clicked link button
int studentid = Convert.ToInt16(e.CommandArgument);
// -- set status in database it is clicked
}
}
after this bind your grid and on rowdatabound find control and set the visibility to true or false (Processed/Not Processed)
Bind your "StatusName" database field to a label and set the visibility to false of Label so that it should not display.
Now take idea from following code
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Display the company name in italics.
Label lblAssignto = (Label)e.Row.FindControl("lblassignto");
LinkButton addevent = (LinkButton)e.Row.FindControl("lnkBtnAddEvent");
LinkButton showevent = (LinkButton)e.Row.FindControl("lnkBtnShowEvent");
if (string.IsNullOrEmpty(lblAssignto.Text))
{
addevent.Visible = false;
showevent.Visible = false;
}
else
{
addevent.Visible = true;
showevent.Visible = true;
}
}
}