click gridview to open new tab with query string

RAVI 896 Reputation points
2024-04-04T15:08:25.84+00:00

Hello

I have two textbox From textbox1 and To Textbox2

This is sample sql data

User will pass from date and to date using textbox1 and textbox2 based on that data will be showing in gridview

for example i pass textbox1 as 01-APR-2023 and Textbox2 as 30-JUL-2023

User's image

For example if i click On APR column 500 value it shoud open new tab with query string like this

www.mywesite.com/default.aspx?DESCTIPTION=UNIT 2 SALE&FROMDATE=01-APR-2023TO=31-APR-2023

Whatever i click from date and to date will be the first and last day of that month

how to achieve like this

gridview sample like this

<asp:TemplateField HeaderText="APR">     <FooterTemplate>         <asp:Label ID="FA1" runat="server" Font-Size="16px" ForeColor="red"></asp:Label><br />         <asp:Label ID="FA11" runat="server" Font-Size="16px" ForeColor="teal"></asp:Label>     </FooterTemplate>     <ItemTemplate>     <asp:Label ID="A1" runat="server" Visible="false" ForeColor="Red" Text='<%#(String.IsNullOrEmpty(Eval("[Apr]").ToString()) ? "0" : Eval("[Apr]"))%>'             ></asp:Label><br />  <asp:Label ID="A11" runat="server"  ForeColor="Black" Font-Size="20px" Text='<%# String.IsNullOrEmpty(Eval("[April VALUE]").ToString()) ? "0" : (String.Format(System.Globalization.CultureInfo.CreateSpecificCulture("hi-IN"), "{0:#,#}" , decimal.Parse(Eval("[April VALUE]").ToString())))%>' >     </asp:Label>     </ItemTemplate>     <FooterStyle Font-Size="26px" ForeColor="Teal" HorizontalAlign="Right" />     <HeaderStyle ForeColor="Black" BackColor="#FCF3CF" Font-Size="20px" />     <ItemStyle ForeColor="Teal" HorizontalAlign="Right" />      <ControlStyle Width="100px" /> </asp:TemplateField>

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,263 questions
{count} votes

Accepted answer
  1. Lan Huang-MSFT 25,556 Reputation points Microsoft Vendor
    2024-04-05T05:29:30.93+00:00

    Hi @RAVI,

    You can attach an OnSelectedIndexChanged event handler to a GridView row using the OnRowDataBound event handler.

    You need to use a virtual LinkButton to render the ASP.Net __doPostBack JavaScript function because we need it to make the row clickable by raising PostBack.

    You must set the property EnableEventValidation = "false"

    In the @Page directive as shown below:

    User's image I wrote a demo based on your needs, you can refer to it.(Regarding the textbox1 and Textbox2 part, I use the example provided before)

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
        <script>
            $(document).ready(function () {
                var currentYear = new Date().getFullYear();
                var currentMonth = new Date().getMonth();
                for (var i = 0; i < 5; i++) {
                    var nextY = currentYear - 1;
                    var value = "01-Apr-" + nextY + "TO" + "31-Mar-" + currentYear;           
                    var text = "Apr" + "-" + nextY + " TO " + "Mar" + "-" + currentYear;
                    $('#financialYear').append(new Option(text, value));
                    currentYear--;
                }
                $('#financialYear').change(function () {
                    var dates = $(this).val().split("TO");
                    $('#<%=FromDate.ClientID%>').val(dates[0]);
                    $('#<%=ToDate.ClientID%>').val(dates[1]);
                })
            })
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <select id="financialYear"></select>
                <div>
                    <asp:TextBox ID="FromDate" runat="server"></asp:TextBox>
                    <asp:TextBox ID="ToDate" runat="server"></asp:TextBox>
                </div>
                <asp:GridView ID="GV1" runat="server" AutoGenerateColumns="False" OnRowDataBound="OnRowDataBound"
        OnSelectedIndexChanged="OnSelectedIndexChanged">
                    <Columns>
                        <asp:TemplateField HeaderText="DESCTIPTION">
                            <ItemTemplate>
                                <asp:Label ID="L3" runat="server" Text='<%# Bind("Description") %>'></asp:Label>
                            </ItemTemplate>
                            <FooterStyle Font-Size="26px" ForeColor="Teal" HorizontalAlign="Right" />
                            <HeaderStyle ForeColor="Black" BackColor="#FCF3CF" Font-Size="20px" />
                            <ControlStyle Width="150px" />
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="APR">
                            <FooterTemplate>
                                <asp:Label ID="FA1" runat="server" Font-Size="16px" ForeColor="red"></asp:Label><br />
                                <asp:Label ID="FA11" runat="server" Font-Size="16px" ForeColor="teal"></asp:Label>
                            </FooterTemplate>
                            <ItemTemplate>
                                <asp:Label ID="A1" runat="server" Text='<%#(String.IsNullOrEmpty(Eval("[APR]").ToString()) ? "0" : Eval("[APR]"))%>'></asp:Label><br />
                            </ItemTemplate>
                            <FooterStyle Font-Size="26px" ForeColor="Teal" HorizontalAlign="Right" />
                            <HeaderStyle ForeColor="Black" BackColor="#FCF3CF" Font-Size="20px" />
                            <ItemStyle ForeColor="Teal" HorizontalAlign="Right" />
                            <ControlStyle Width="100px" />
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="MAY">
                            <FooterTemplate>
                                <asp:Label ID="FA2" runat="server" Font-Size="16px" ForeColor="red"></asp:Label><br />
                                <asp:Label ID="FA22" runat="server" Font-Size="16px" ForeColor="teal"></asp:Label>
                            </FooterTemplate>
                            <ItemTemplate>
                                <asp:Label ID="A2" runat="server" Text='<%#(String.IsNullOrEmpty(Eval("[MAY]").ToString()) ? "0" : Eval("[MAY]"))%>'></asp:Label><br />
                            </ItemTemplate>
                            <FooterStyle Font-Size="26px" ForeColor="Teal" HorizontalAlign="Right" />
                            <HeaderStyle ForeColor="Black" BackColor="#FCF3CF" Font-Size="20px" />
                            <ItemStyle ForeColor="Teal" HorizontalAlign="Right" />
                            <ControlStyle Width="100px" />
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
                <asp:LinkButton ID="lnkDummy" runat="server"></asp:LinkButton>
            </div>
        </form>
    </body>
    </html>
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            DataTable hdt = new DataTable();
            SqlConnection hcon1 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConString"].ConnectionString);
            hcon1.Open();
            SqlCommand hcmd1 = new SqlCommand("select * from Record ", hcon1);
            SqlDataAdapter hada1 = new SqlDataAdapter(hcmd1);
            hada1.Fill(hdt);
            GV1.DataSource = hdt;
            GV1.DataBind();
            hcon1.Close();
        }
    }
    protected void OnRowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(GV1, "Select$" + e.Row.RowIndex);
            e.Row.ToolTip = "Click to select this row.";
        }
    }
    protected void OnSelectedIndexChanged(object sender, EventArgs e)
    {
        int index = GV1.SelectedRow.RowIndex;
        System.Web.UI.WebControls.Label DESCTIPTION = (System.Web.UI.WebControls.Label)GV1.SelectedRow.FindControl("L3");
        string url = "default.aspx?DESCTIPTION=" + DESCTIPTION.Text + "&FROMDATE=" + FromDate.Text + "TO=" + ToDate.Text;
        ////www.mywesite.com/default.aspx?DESCTIPTION=UNIT 2 SALE&FROMDATE=01-APR-2023TO=31-APR-2023        
        Response.Redirect(url);     
    }
    

    User's image

    Note:You'll notice that spaces in the redirected URL are encoded as %20. To avoid your misunderstanding, let me explain:

    You can't have a valid URL with spaces, the space character is actually illegal in an URL.

    You can't make the Response.Redirect method avoid encoding the spaces, it's not designed to create an illegal URL.
    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. RAVI 896 Reputation points
    2024-04-13T07:22:20.0033333+00:00

    Hello

    My Gridview inside table and td then gridview is this error due to thi

    Server Error in '/STAR' Application.


    Input string was not in a correct format.

    __Description:__An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. __Exception Details:__System.FormatException: Input string was not in a correct format. Source Error:

    |Line 1236:
    Line 1237: int index = GridView1.SelectedRow.RowIndex;
    Line 1238: Int32 columnId = Int32.Parse(this.hfColumnId.Value);
    Line 1239: string header = GridView1.Columns[columnId].HeaderText;| | -------- | |Line 1236: Line 1237: int index = GridView1.SelectedRow.RowIndex; Line 1238: Int32 columnId = Int32.Parse(this.hfColumnId.Value); Line 1239: string header = GridView1.Columns[columnId].HeaderText;|

    0 comments No comments