my grid view is this i want to when i click Approve button it insert in to my db
<asp:GridView ID="gridview1" runat="server" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" Height="222px" Width="451px">
<Columns>
<asp:ButtonField CommandName="Approve" Text="Approve" ButtonType="Button" />
<asp:ButtonField CommandName="Rejected" ButtonType="Button" Text="Disapprove" />
</Columns>
</asp:GridView>
Add OnRowCommand Event handler to your grid as here:
<asp:GridView ID="gridview1" runat="server" OnRowCommand="gridview1_RowCommand" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" Height="222px" Width="451px">
<Columns>
<asp:ButtonField CommandName="Approve" Text="Approve" ButtonType="Button" />
<asp:ButtonField CommandName="Rejected" ButtonType="Button" Text="Disapprove" />
</Columns>
</asp:GridView>
and add following codes for handling approve command button on click:
protected void gridview1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.ToLower() == "approve")
{
//Code for inserting items in database goes here.
}
}
Related
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>
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;
}
}
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")
{
}
}
I have a gridview in my web page. I a linkbutton column. The commandName of the linkbutton column is "lbtnedit", I want When i click the linkbutton another tab will show detail of Resume by loading ID, but when i click linkbutton it don't run into gvresume_OnRowCommand
Thanks in advance !
Here my Grid:
<asp:UpdatePanel ID="udpsubtabResumeList" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<div class="settingrow">
<div class="gSDMS_Grid">
<asp:GridView ID="gridViewResume" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" Width="100%"
AllowPaging="true" AllowSorting="true" AutoGenerateColumns="false" PageSize="10" PagerSettings-Position="Bottom" PagerStyle-HorizontalAlign="Right"
CssClass="css_grid" OnItemCommand="gvresume_OnRowCommand" EnableViewState="true"
>
<AlternatingRowStyle CssClass='AlternatingRowStyle' />
<EditRowStyle CssClass='EditRowStyle' />
<FooterStyle CssClass='FooterStyle' />
<HeaderStyle CssClass='HeaderStyle' />
<PagerStyle CssClass='PagerStyle' HorizontalAlign="Right" />
<RowStyle CssClass='RowStyle' />
<SelectedRowStyle CssClass='SelectedRowStyle' />
<Columns>
<asp:TemplateField HeaderText="Full Name" ItemStyle-CssClass="txt" SortExpression="Fullname">
<ItemTemplate><%#Eval("Fullname")%></ItemTemplate></asp:TemplateField>
<asp:TemplateField HeaderText="View" ItemStyle-CssClass="edit-del accept" ItemStyle-HorizontalAlign="Center">
<ItemTemplate><a class="edit" href='<%# "/FutureEmployee/PostResume.aspx?&id=" + Eval("ResumeID") %>' title="Detail"> </a>
</ItemTemplate></asp:TemplateField>
<asp:TemplateField HeaderText="Edit" ItemStyle-CssClass="edit-del accept">
<ItemTemplate><asp:LinkButton runat="server" ID="lbtnedit" Text="Edit" CommandName="edit_cmd" CommandArgument='<% #Eval("ResumeID") %>'></asp:LinkButton>
</ItemTemplate></asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</div>
</ContentTemplate>
</asp:UpdatePanel>
Here is event
protected void gvresume_OnRowCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName=="edit_cmd")
{
_id = new Guid(e.CommandArgument.ToString());
Response.Redirect(SiteRoot + "/FutureEmployee/EmployeeTab.aspx#subTabViewResume");
}
}
Edit one:
when i use firebug here is content in link:
href="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$aaaaaaa$bbbbbbbbbbb", ""false, "",";Clients.aspx", false, true)
I think it fine if : javascript:__doPostBack('ctl00$mainContent$gridViewResume','resumeID') but i don't know how to do this ?
Asp:GridView hasn't event OnItemCommand.
You should use OnRowCommand and change signature of handler:
protected void gvresume_OnRowCommand(Object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "edit_cmd")
{
}
}
I solved proplem by remove all validate in my page.
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
}