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:
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);
}
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.