You must Sign In to post a response.
  • Category: ASP.NET

    How to Redirect Another Page with GridView Item Template Control Value

    Am creating a project.
    On my project in a page i am having 3 Links in a GridView.
    1.ACCEPT
    2.REJECT
    2.EDIT
    if i am click any link the function should be done. & create session value also.

    i am using First 2 following code

    1.ACCEPT am using
    .ASPX
    OnRowUpdating="cgv_RowUpdating"

    .CODE BEHIND

    protected void cgv_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
    Label lblgvenquiryid = (Label)cgv.Rows[e.RowIndex].FindControl("lblenquiryid");
    Session["Session_enquiryid"] = lblgvenquiryid.Text.Trim();
    }

    2.REJECT am using
    .ASPX
    OnRowDeleting="cgv_RowDeleting"

    .CODE BEHIND

    protected void cgv_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
    Label lblgvenquiryid = (Label)cgv.Rows[e.RowIndex].FindControl("lblenquiryid");
    Session["Session_enquiryid"] = lblgvenquiryid.Text.Trim();
    }

    3.REJECT am using

    OnRowCommand :
    Error:Index was out of range

    LinkButton:
    Error :Input String was not a correct Format.

    Button:

    Unable to cast object of type 'System.Web.UI.WebControls.ImageButton' to type 'System.Web.UI.WebControls.Button'.


    Exception Details: System.InvalidCastException: Unable to cast object of type 'System.Web.UI.WebControls.ImageButton' to type 'System.Web.UI.WebControls.Button'.

    Source Error:


    protected void imgbtnUpdate_Click(object sender, ImageClickEventArgs e)
    {
    GridViewRow Row = ((Button)sender).Parent.Parent as GridViewRow;
    int index = Row.RowIndex;
    Session["Session_Enquiry_Id"] = ((Label)cgv.Rows[index].FindControl("lblenquiryid")).Text;
    }

    If i am click EDIT link or a button it should go to another page with only my created session value.

    So i don't know how to done third one and am struggling.
    So anyone know please help me to i am done this task.


    Thanks With
    Paul.S
  • #765159
    Thanks All OF You , I got exavt output for my problem.

    My Code is

    .Aspx
    <asp:TemplateField HeaderText="MODIFY">
    <ItemTemplate>
    <asp:LinkButton ID="lnkbtn" runat="server" Text="EDIT" OnClick="lnkbtn_Click"></asp:LinkButton>
    </ItemTemplate>
    </asp:TemplateField>

    Code Behind:

    protected void lnkbtn_Click(object sender, EventArgs e)
    {
    var lb = (LinkButton)sender;
    var row = (GridViewRow)lb.NamingContainer;
    if (row != null)
    {
    var lblStatus = row.FindControl("lblenquiryid") as Label;
    var lblfr = row.FindControl("lblfr_id") as Label;
    Session["Session_Enquiry_Id"] = lblStatus.Text.ToString();
    Session["Session_FR_Id"] = lblfr.Text.Trim();
    Response.Redirect("FRSheetModify.aspx");
    }
    }

    Thanks With

    Paul.S

  • #765160

    Hi Paul,

    The main reason for the following error,

    "Error:Index was out of range", If the index limit is 10 and you are trying to call 11th index that time you got this type of error.

    "Error :Input String was not a correct Format.", for example if you try to convert the data into one particular format, but the input string is not a convertible, that time you got this type of error.

    Ex: You are trying to convert date format into integer that time you got this error.

    To resolve those errors, my suggestion is put a break point and debug the code and check line by line and in which line you got this error, that line you can check the conversion and correct it.


    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #765162
    Thanks Mr.Naveen

    Paul.S

  • #765169
    IndexOutOfRange exception will throw when you are accessing an array/list index item which is not exist in that collection, in your case Index is not exist in gridview
    secondly, you got 'Unable to cast object of type ' cause you are trying to convert Button Image to button class, which is not possible in anyways, you need to Implement GridView RowCommand Event to handle ImageButton Click Event
    may be following links will help you more
    http://codedisplay.com/how-to-use-imagebutton-columns-in-gridview-in-asp-net-c-vb-net/
    http://stackoverflow.com/questions/10353115/add-a-clickable-image-to-a-gridview-in-asp-net

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]


Sign In to post your comments