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.
Related
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.
}
}
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>
i use gridview and UpdatePanel in asp.net.
<asp:UpdatePanel ID="upSubject" runat="server">
<ContentTemplate>
<div class="c_box" style="height: auto; text-align: center; margin-top: 10px; width:565px; padding-right:140px;">
<asp:GridView ID="gvSubject" runat="server" AutoGenerateColumns="False"
SkinID="gvSubject" ShowHeader="true" ShowHeaderWhenEmpty="true"
onrowcommand="gvSubject_RowCommand">
<Columns>
<asp:BoundField DataField="lblCounter" HeaderText="Number" ReadOnly="True" HeaderStyle-Font-Bold="false" ItemStyle-Width="60px"></asp:BoundField>
<asp:BoundField DataField="lblSubject" HeaderText="Tiltle" ReadOnly="True" HeaderStyle-Font-Bold="false" ItemStyle-Width="300px" ItemStyle-HorizontalAlign="Right"></asp:BoundField>
<asp:TemplateField HeaderText="Delete" ItemStyle-HorizontalAlign="Center" HeaderStyle-Font-Bold="false" ItemStyle-Width="60px">
<ItemTemplate>
<asp:ImageButton ID="dltSubject" runat="server" CommandName="remove" CommandArgument="lblCounter" ValidationGroup="Subject" OnClientClick="return confirm('Delete?')" ImageUrl="~/Style/Pic/pic_delete_small.png" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#000000" />
<EmptyDataRowStyle BackColor="#00ff00" />
<EmptyDataTemplate>
Not item...
</EmptyDataTemplate>
</asp:GridView>
</div>
</ContentTemplate>
<Triggers>
<asp:asyncpostbacktrigger controlid="gvSubject" eventname="RowCommand" />
</Triggers>
</asp:UpdatePanel>
or
<asp:asyncpostbacktrigger controlid="dltSubject" eventname="Click" />
.
protected void gvSubject_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "remove")
{
DataTable _dt = (DataTable)Session["Subject"];
_dt.Rows[int.Parse(e.CommandArgument.ToString())].Delete();
gvSubject.DataSource = _dt;
gvSubject.DataBind();
Session["Subject"] = _dt;
}
}
but not work UpdatePanel. please help me for work button gridview in UpdatePanel.
i want work ImageButton in gridview and UpdatePanel.
You dont have to use Triggers for the RowCommand. Also remove rowcommand if you dont want to use besides this button click.
Please specify OnCommand=dtlSubject_Click
I have used here "Number" as commandargument thinking that it is your unique id needed to delete the record.
You need to specify unique column for deletion in commandargument.
<asp:ImageButton ID="dltSubject" runat="server"
CommandArgument='<%#DataBinder.Eval(Container.DataItem, "Number")%>'
ValidationGroup="Subject" OnCommand="dtlSubject_Click"
OnClientClick="return confirm('Delete?')" ImageUrl="~/Style/Pic/pic_delete_small.png" />
And on server side write down following code :
protected void dtlSubject_Click(object sender, CommandEventArgs e)
{
string uniqueid= e.CommandArgument.ToString();
//call your procedure here to delete your item.
}
I am having trouble when trying to fire a button in a GridView with the parameter CommandName = "x", I try to reach my "If" in the GridView1_RowCommand event but I just cant for some reason, if you could help me I'd be gratefull .
This is my .aspx part
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" CellPadding="4"
EnableModelValidation="True" ForeColor="#333333" GridLines="None"
Height="193px" Width="968px" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" DataKeyNames="ID"
DataSourceID="SOURCE1" onrowcommand="GridView1_RowCommand">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Button Text = "Seleccionar" runat="server" CommandName="X" CommandArgument='<%# Container.DataItemIndex %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="IDEmpresa" HeaderText="IDEmpresa"
SortExpression="IDEmpresa" />
</Columns>
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
And this is my C# code:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "X")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = GridView1.Rows[index];
Label1.Text = "WOW It reached out";
}
}
I followed the instructions of the ASP.net page, and im fairly new to .net (I dont know if the UpdatePanel has something to do with it)
Doing a quick test, the code you have provided works, but I did have to wire up my own datasource.
What you are missing here is that Label1 is outside of your UpdatePanel and will not refresh based on your localized postback within the UpdatePanel.
A word of caution with GridViews and UpdatePanels/Buttons. Make sure you are not manually binding/rebinding during Page_Load, and if you have code to do so, you do so within an if(!IsPostBack) { } statement.
I have a Default.aspx page in which I have bind a Grid. In the Grid is a button named Details.
I also have a Details.aspx which has a GridView. If I click the button that exist in Default.aspx, there appears an Details.aspx page which appears empty.
When click the Detail button of a specific row, there appears the details of that button clicked. I am not understanding on how to pass the ID to Details.aspx.
Can anybody guide me to this please?
Details.aspx
<asp:GridView ID="DetailsGridView" runat="server" BackColor="White" AutoGenerateColumns="false"
BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" DataKeyNames="ID"
ForeColor="Black" GridLines="Vertical">
<FooterStyle BackColor="#CCCC99" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
Default.aspx:
<Columns>
<asp:BoundField HeaderText="First Name" DataField="FirstName" />
<asp:BoundField HeaderText="Last Name" DataField = "LastName" />
<asp:BoundField HeaderText="HomePhoneNumber" DataField="HomePhoneNumber" />
<asp:TemplateField HeaderText="ViewDetails">
<ItemTemplate>
<asp:Button ID="Deatils" runat="server" Text="Details" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Actions">
<ItemTemplate>
<asp:Button ID="Modify" runat="server" Text="Modify" />
<asp:Button ID="Delete" runat="server" Text="Delete" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCC99" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</div>
JavaScript
<script type="text/javascript">
function viewProfile(index)
{
var GridID = document.getElementById("PersonGridView");
var row=GridID.rows[parseInt(index)+1];
window.open('Details.aspx?coaid'+row);
}
</script>
Code Behind of Default.aspx:
protected void PersonGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var DataKeys = PersonGridView.DataKeys[e.Row.RowIndex];
DataKeys.Value.ToString();
var ID = DataKeys["ID"].ToString();
Button btnDetails = (Button)e.Row.FindControl("Deatils");
Button btnModify = (Button)e.Row.FindControl("Modify");
Button btnDelete = (Button)e.Row.FindControl("Delete");
btnModify.CommandName = "Modify";
btnDelete.CommandName = "Delete";
btnDetails.CommandName = "Deatils";
btnDelete.CommandArgument = btnModify.CommandArgument = btnDetails.CommandArgument = string.Format("{0}", ID);
btnDetails.Attributes["onclick"] = string.Format("viewProfile({0}); return false;", e.Row.RowIndex);
}
}
EDIT: I've modified my example to not use a HyperLink and instead will build a LinkButton that can navigate to your Details.aspx page passing the ID value in the QueryString:
Default.aspx:
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton CommandName="Details" CommandArgument='<%# Eval("ID") %>' Text="Details" runat="server" />
</ItemTemplate>
</asp:TemplateField>
Default.aspx.cs:
protected void PersonGridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Details")
{
Server.Transfer("Details.aspx?ID=" + e.CommandArgument.ToString());
}
}
On the Grid when you click the button
do the following
public protected sub Button_Click(EventArgs e)
{
if (grid.SelectedItem != null)
{
var selectedRow = grid.SelectedItem ;
// do what ever you want to Selected Row that may be binding it to another Form etc.
}
Do the same for OnSelectedEvent if you want to do it that way instead.
public protected sub OnSelectedEvent_Click(EventArgs e)
{
if (grid.SelectedItem != null)
{
var selectedRow = grid.SelectedItem ;
// do what ever you want to Selected Row that may be binding it to another Form etc.
}