asp gridview button another command - asp.net

I have a Gridview based on a Acces DB in .aspx
I added +1 column to the grid, which is:
<asp:TemplateField HeaderText="view">
<ItemTemplate>
<asp:LinkButton runat="server" Text="Select" CommandName="Select" CausesValidation="False" id="Button1"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
I have a button outside of the grid: Button2
Could I add a command to Button1, to simulate to click on Button2 as well? Thank you, regards.

Did I get you correctly:
void ContactsGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if(e.CommandName=="Select")
{
MyFunction();
}
}
void Button2_Click(Object sender, EventArgs e)
{
MyFunction();
}
void MyFunction(){
//your code
}

Related

button OnItemCommand not firing inside asp:Repeater

I have a web forms project where i have the following button wrapped in an asp:Repeater
<asp:Repeater ID="rptBookingSlots" OnItemCommand="BookingSlotOnItemCommand" runat="server">
<ItemTemplate>
<tr>
<td>
<asp:Button ID="Button3" CssClass="Delete" CommandName="delete" CommandArgument="<%# (Container.DataItem as RemoveGroup).Id %>" runat="server" Text="Delete" OnClientClick="return confirm('Are you sure you want to permanently delete this record);" />
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
But when I select the button via the front end it never hits the method I have specified in my code behind
protected void BookingSlotOnItemCommand(object source, RepeaterCommandEventArgs e)
{
switch (e.CommandName)
{
case "delete": //do work to delete record
}
}
I have a page_preRender method which is always hit on the post pack
protected void Page_PreRender(object sender, EventArgs e)
{
if (IsPostBack && !_dataBound)
{
BindAllData();
}
}
Can anyone explain why my onItemCommand method wont get hit when im attached to the process, i am unable to delete records?
Thanks
The way I would do this would be to load the data in the page_load rather than the pre_render.
I have tested this and the button command is stepped into
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
var myList = new List<string> {"foo", "bar"};
rptBookingSlots.DataSource = myList;
rptBookingSlots.DataBind();
}
}

asp button on click inside item template not firing

Not able to make this work.
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnApprove" runat="server" Text="Approve" OnClick ="btnApprove_Click" />
</ItemTemplate>
</asp:TemplateField>
code behind:
protected void btnApprove_Click(object sender, EventArgs e)
{
Response.Redirect("viewprofile.aspx");
}
not even firing when button is clicked. any tricks on this?
Set EnableEventValidation="false" right at the top in your Page directive:
<%# Page EnableEventValidation="false" Language="C#"...
Just beware that setting this value to false can expose your website to security vulnerabilities.As an alternative, instead of setting EnableEventValidation="false" you can handle the grid views OnRowCommand:
.ASPX:
<asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button runat="server" Text="Approve" CommandName="Approve" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code behind:
public partial class delete_me : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)//THIS IS IMPORTANT.GridView1_RowCommand will not fire unless you add this line
{
var p1 = new Person() { Name = "Person 1" };
var p2 = new Person() { Name = "Person 2" };
var list = new List<Person> { p1, p2 };
GridView1.DataSource = list;
GridView1.DataBind();
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
System.Diagnostics.Debugger.Break();
}
}
public class Person
{
public string Name { get; set; }
}
You Just put on your gridview.
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click"/>
</ItemTemplate>
</asp:TemplateField>
Also put code behind
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("WebForm1.aspx");
}
Try!!!! it's working fine...

Get value of gridview.selected.row[] with visible property = 'false'

Hi from this field in my gridview, I'd like to pass the id value when the select command field is clicked, but i don't want to have the id field visible so I have the visible property set to false; however, when I pass it on the SelectedIndexChanged event the value is "" yet when the visible property is set to "true" the text value passes fine. What is the correct way to do this?
<asp:BoundField DataField="Project_ID" Visible="false"/>
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
String ProjID = GridView1.SelectedRow.Cells[10].Text;
}
Try this:
.aspx
<asp:Gridview id="GridView1" runat="server" DataKeyNames="Project_ID" />
.cs
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
if(gridView.SelectedDataKey != null)
{
var selectedId = GridView1.SelectedDataKey.Value;
}
}
Here you'll find more info about DataKeys in Gridviews: http://msdn.microsoft.com/de-de/library/system.web.ui.webcontrols.gridview.selecteddatakey(v=vs.110).aspx
I have done something like this in winform maybe can help you. That is what i used
int rowindex = dataGridView1.CurrentRow.Index;
string ProjID= dataGridView1.Rows[rowindex].Cells[10].Value.ToString();
you could use HiddenField inside your gridView ItemTemplate to keep the ID and use it in onrowcommand event, like below:
.aspx
<asp:GridView ID="gridProject" runat="server"
onrowcommand="gridProject_RowCommand">
<Columns>
<ItemTemplate>
<asp:HiddenField ID="hidProjectID" runat="server"
Value='<%# ((DataRowView)Container.DataItem)["Project_ID"] %>' />
<asp:Button ID="btnProject" runat="server" Text="use pro id"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
CommandName="DoSomething"></asp:Button>
</ItemTemplate>
</Columns>
</asp:GridView>
aspx.cs
protected void gridProject_RowCommand(object sender, GridViewCommandEventArgs e)
{
int index = 0;
GridViewRow gridRow;
GridView grid = sender as GridView;
try
{
switch (e.CommandName)
{
case "DoSomething":
index = Convert.ToInt32(e.CommandArgument);
row= gridProject.Rows[index];
string Id = ((HiddenField)row.FindControl("hidProjectID")).Value;
//do whatever you want here
break;
// and you can have as many commands as you want here
}
}
catch { //display error }
}

CheckBox in GridView firing CheckedChanged event at wrong time

I have a checkbox in my gridview footer with autopostack=true (which should fire a checkedchanged event), and a linkbutton in my itemtemplate (which should fire the gridview rowcommand event).
Everything has been working fine, until I placed the following code in my gridview rowdatabound (or databound) event:
for (int i = 0; i < gridCartRows.Columns.Count - 2; i++)
{
e.Row.Cells.RemoveAt(0);
}
e.Row.Cells[0].ColumnSpan = gridCartRows.Columns.Count - 1;
Now, when I click my linkbutton, the checkbox checkedchanged event is automatically fired, and then the rowcommand event is fired.
Why is the checkedchanged event being fired when it shouldn't, when I add the above code?
Is there a way to get around this?
Edit
Here's a sample, that reproduces my issue:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" ShowFooter="True"
OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Column1">
<ItemTemplate>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Column2">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" Text="Fire Row Command" CommandName="Fire" />
</ItemTemplate>
<FooterTemplate>
Footer
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" OnCheckedChanged="CheckBox1_CheckedChanged" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Column3">
<ItemTemplate>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code-behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.DataSource = new int[5];
GridView1.DataBind();
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Footer)
{
for (int i = 0; i < GridView1.Columns.Count - 2; i++)
{
e.Row.Cells.RemoveAt(0);
}
e.Row.Cells[0].ColumnSpan = GridView1.Columns.Count - 1;
((CheckBox)e.Row.FindControl("CheckBox1")).Checked = true;
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Fire")
{
Response.Write("RowCommand fired.");
}
}
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
Response.Write("CheckBox fired.");
}
Note, I'm setting the CheckBox property to true in the RowDataBound - if I remove this, it works fine. So merging the cells and setting the checkbox property aren't working nicely together.
GridView Control events are stored in viewstate and they can get messed up very easily. Here removing a cell causing to bind different event to link button. Although after postback __EVENTTARGET is LinkButton, wrong method is called and wrong control, event arguments are passed.
I would suggest to hide the cell rather than removing it:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[1].ColumnSpan = GridView1.Columns.Count - 1;
e.Row.Cells[0].Visible = false;
((CheckBox)e.Row.FindControl("CheckBox1")).Checked = true;
}
}
This will serve the purpose.

File upload button for each row inside gridview and button to upload in asp.net

I need to have file upload button for each row in grid view. Finally I need a button to upload the files selected from fileupload button. I have a code, but it has both file upload button and button to update, however if I change the command name it is not working. Below is my code.
In my code I am displaying both fileupload buttons and buton updates. This works fine, but I need multiple file upload buttons with single button to update all the files.
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" CommandArgument='<%# Container.DataItemIndex %>'
Text="Upload" OnClick="Button1_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string[] strArray = { "Test1", "Test2", "Test3" };
GridView1.DataSource = strArray;
GridView1.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
int index = int.Parse(((Button)sender).CommandArgument);
FileUpload file = (FileUpload)GridView1.Rows[index].FindControl("FileUpload1");
if (file != null)
{
if (file.HasFile)
{
Response.Write(file.PostedFile.FileName);
Response.End();
//file.SaveAs(Server.MapPath("~") + "\\DataBind\\" + System.IO.Path.GetFileName(file.PostedFile.FileName));
}
}
}
It looks like you're close. You just want to do the same thing on all the rows. You can use a foreach loop for that:
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
FileUpload file = (FileUpload)row.FindControl("FileUpload1");
if (file != null)
{
if (file.HasFile)
{
// Save your file here
}
}
}
}

Resources