Button Header and Check box field in Gridview asp.net - asp.net

I want a button in the Header of my Gridview and a checkbox field in the row of that header.
I was hoping the following code will work. However, this is not working. I just see "Remove" text in my header and when I click it nothing happens.
Am I doing something wrong?
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false" CellPadding="4" ForeColor="#333333"
GridLines="None" OnRowCommand="Gridview2_RowCommand">
<Columns>
<asp:BoundField DataField="fname" HeaderText="First Name" />
<asp:BoundField DataField="mname" HeaderText="Middle Name" />
<asp:BoundField DataField="lname" HeaderText="Last Name" />
<asp:TemplateField HeaderText="Remove">
<ItemTemplate>
<asp:CheckBox ID="checkselect" runat="server" />
</ItemTemplate>
<asp:HeaderTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Split">sort</asp:LinkButton>
</asp:HeaderTemplate>
</asp:TemplateField>
</Columns> </Gridview>
The Even for Row Command
protected void Gridview2_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Split")
{
Response.Write("I was clicked");
}
}
Can someone help me with this?

I was using the wrong tab prefix. The header template is simply
<HeaderTemplate> not <asp:HeaderTemplate>

Related

Adding a new column with buttons in gridview and binding data to it

I have added a button for Print in my gridview table as follows
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="ID"
DataSourceID="SqlDataSource2"
onselectedindexchanged="GridView1_SelectedIndexChanged" Width="522px">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="Story_number" HeaderText="Story_number"
SortExpression="Story_number" />
<asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" />
<asp:BoundField DataField="Memory_card" HeaderText="Memory_card"
SortExpression="Memory_card" />
<asp:BoundField DataField="Story_Name" HeaderText="Story_Name"
SortExpression="Story_Name" />
<asp:ButtonField ButtonType="Button" Text="print" />
</Columns>
</asp:GridView>
Please help me with the c# code for this button. When the button is pressed I need it to redirect to a page (print.aspx). I have been trying the following code but it does not work .Thanks in advance for your help.
Session["id"] = GridView1.SelectedRow.Cells[0].Text;
Response.Redirect("Print.aspx");
Instead of buttonField, use template field
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="ButtonPrint" runat="server" CssClass="yourCssClassIsNeedIt" OnClick="printRegFunction"
CommandArgument='<%# Bind("id") %>' ImageUrl="images/button.png"/>
</ItemTemplate>
</asp:TemplateField>
your server code or behind code here:
protected void printRegFunction(object sender, ImageClickEventArgs e)
{
Session["id"] = ((ImageButton)sender).CommandArgument;
Response.Redirect("Print.aspx");
}

How to get the Textbox value from DataGridView?

So here I'm trying to get the Textbox filled with the selected data from the DataGridView, so when I clicked the button in the DataGridView, the selected result will be put into the Textbox, I've tried the solution from other resource but still no luck. Can somebody help? Here's the DataGridView code:
DataGridView:
<asp:Panel ID="PanelDGV" runat="server" ScrollBars="None" Height="250" Width="515">
<asp:GridView ID="DGV" runat="server" AutoGenerateColumns="False" GridLines="None" AllowPaging="true" PageSize="8" CssClass="mGrid" PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt">
<Columns>
<asp:BoundField DataField="ProjectCode" HeaderText="Project Code" />
<asp:BoundField DataField="ProjectName" HeaderText="Project Name" />
<asp:ButtonField ButtonType="Image" ImageUrl="../Support/Image/Edit.png" ItemStyle-HorizontalAlign="Center" CommandName="CmdSearch" HeaderText="Edit">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:ButtonField>
</Columns>
<PagerStyle CssClass="pgr"></PagerStyle>
AlternatingRowStyle CssClass="alt"></AlternatingRowStyle>
</asp:GridView>
</asp:Panel>
notice the button is:
<asp:ButtonField ButtonType="Image" ImageUrl="../Support/Image/Edit.png" ItemStyle-HorizontalAlign="Center" CommandName="CmdSearch" HeaderText="Edit">
oh, and the textbox's ID is "TbProjectCode", and both are in separate pages, say that 1.aspx contain the textbox and a button to open the datagridview and 2.aspx contain the datagridview and the button to select the Project Code.
thank you
Add OnRowCommand event on your gridview: OnRowCommand="DGV_OnRowCommand".
Then add this on your code behind:
protected void DGV_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "CmdSearch")
{
int index = (int)e.CommandArgument;
GridViewRow row = DGV.Rows[index];
// Get the text on first cell of the row which is the project code.
TbProjectCode.Text = row.Cells[0].Text;
}
}

Edit button text in GridView

Q> I want to show a GridView button's text as hard coded and a event to fire on button click. How to achieve this ?
Till now I've been able to come this far
But I want to show button text as Read or Delete not the value in the Read/Delete column.
The code I've used
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="MID" DataSourceID="inbox" EnableModelValidation="True"
onselectedindexchanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:ButtonField ButtonType="Button" DataTextField="MID" HeaderText="Read"
Text="Read" />
<asp:BoundField DataField="MID" HeaderText="MID" InsertVisible="False"
ReadOnly="True" SortExpression="MID" />
<asp:BoundField DataField="sender" HeaderText="sender"
SortExpression="sender" />
<asp:BoundField DataField="subject" HeaderText="subject"
SortExpression="subject" />
<asp:BoundField DataField="on" HeaderText="on" SortExpression="on" />
<asp:ButtonField ButtonType="Button" DataTextField="MID" HeaderText="Delete"
Text="Delete" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="inbox" runat="server"
ConnectionString="<%$ ConnectionStrings:connectionString %>"
SelectCommand="SELECT [MID], [sender], [subject], [on] FROM [mail]">
</asp:SqlDataSource>
If you want the text to appear as "Delete" or "Read", then simply don't set the DataTextField property to use the MID property of the result and instead set the CommandName property as so:
<asp:ButtonField ButtonType="Button" CommandName='<%#Eval("MMID")%>' HeaderText="Delete"
Text="Delete" />
As far as handling the OnClick event of the buttons, you can handle the OnRowCommand event on the GridView as so:
<asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView_RowCommand" AutoGenerateColumns="False"
Now add the code behind:
protected void GridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
string MMID = e.CommandName;
if( (e.CommandSource as ButtonField).Text=="Delete")
{
//oh, I should delete this MMID
}
}
UPDATE
Above code does not work. ButtonField is as useful as nipples are to men. Instead use an ItemTemplateField as so:
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btn" runat="server" CommandName="Delete" CommandArgument='<%#Eval("MID") %>'
Text="Delete" />
</ItemTemplate>
</asp:TemplateField>
Then the GridView_RowCommand becomes this:
protected void GridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
string mid = e.CommandArgument.ToString();
// If multiple buttons are used in a GridView control, use the
// CommandName property to determine which button was clicked.
if (e.CommandName == "Delete")
{
}
}

Get the ID of a record in a gridview asp.net

i have a checkbox in a template field of a gridview and i want to get the id of the record at a checkbox tick. how do i do it? i am doing asp.net and also by using datakeynames instead, my gridview also shows the datakeynames persID column. why?
my code:
<asp:GridView ID="GridViewHostelMember" runat="server" DataKeyNames="_PersID" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="_PersID" HeaderText="_PersID" />
<asp:BoundField DataField="FirstName" HeaderText="First Name" />
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:BoundField DataField="DOB" HeaderText="DOB" />
<asp:BoundField DataField="FatherName" HeaderText="FatherName" />
<asp:BoundField DataField="Type" HeaderText="Type" />
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBoxSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow item in this.GridViewHostelMember.Rows)
{
CheckBox chbTemp = item.FindControl("CheckBoxSelect") as CheckBox;
if (chbTemp != null)
{
if (chbTemp.Checked)
{
Label1.Text = item.Cells[0].Text;
}
}
}
}
_PersID is showing because autogeneratecolumns is set to true; if true, it shows all columns. To hide, set to false, and explicitly add the columns to the grid.
For the first part, at checkbox tick, you would need to set AutoPostBack="true" on the checkbox, which posts back, and then you can check the data key for the current row of the grid.
Reference -
my gridview also shows the datakeynames persID column. why?
true to automatically create bound fields for each field in the data
source; otherwise, false. The default is true.
It should be like below..
<asp:GridView ID="GridViewHostelMember" autogeneratecolumns="False"
runat="server" DataKeyNames="_PersID">
i want to get the id of the record at a checkbox tick. how do i do it?
Sample Code
protected void CheckBox_Checked(object sender, EventArgs e)
{
CheckBox c = (CheckBox)sender;
//c.ValidationGroup is your ID
}
Sample HTML
<asp:GridView ID="ed" runat="server" OnRowCommand="GridView_RowCommand">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chk" runat="server" AutoPostBack="true" ValidationGroup='<%#Eval("ID") %>' OnCheckedChanged="CheckBox_Checked" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

How do I add a column with buttons in to a gridview in asp.net?

As my gridview is populating I want to add an extra column with some buttons in but I can't seem to figure out how, or what might be the best way. Can anyone get me started?
Use a Template Column
<asp:GridView ID="GridView1" runat="server" DataKeyNames="id" DataSourceID="SqlDataSource1"
OnRowCommand="GridView1_OnRowCommand">
<Columns>
<asp:BoundField DataField="name" HeaderText="Name" />
<asp:BoundField DataField="email" HeaderText="Email" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Button ID="Button1" runat="server" CausesValidation="false" CommandName="SendMail"
Text="SendMail" CommandArgument='<%# Eval("id") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void GridView1_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName != "SendMail") return;
int id = Convert.ToInt32(e.CommandArgument);
// do something
}

Resources