Hello, I have a Panel, dropdownlist, textbox and gridview in my design.
I have search code in textbox. While I enter any text in that textbox, I will get that
result in gridview. After displaying the results in that gridview, When I click on any
of that particular row, I need to get that data in a table.
(Table consists of 3 textboxes).
Here is the code. .
Table:
<table>
<tr>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
<td>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</td>
</tr>
</table>
Panel, Dropdownlist and gridview:
<asp:Panel ID="Panel2" runat="server">
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
CssClass="style1" Height="38px" Width="328px">
<asp:ListItem>Code</asp:ListItem>
<asp:ListItem>Description</asp:ListItem>
</asp:DropDownList><br />
<asp:TextBox ID="TextBox1" runat="server" ontextchanged="TextBox1_TextChanged"
AutoPostBack="True" Height="23px" Width="330px"></asp:TextBox>
<asp:GridView ID="GridView1" runat="server"
OnRowDataBound="GridView1_RowDataBound"
onselectedindexchanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:CommandField ShowSelectButton="true" ItemStyle-CssClass="visible"/>
</Columns>
</asp:GridView>
</asp:Panel>
In cs file:
protected void Page_Load(object sender, EventArgs e)
{
cn.Open();
string s = "select * from Item_tbl";
SqlCommand cmd = new SqlCommand(s, cn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
cmd.ExecuteNonQuery();
cn.Close();
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
try
{
cn.Open();
if (DropDownList1.SelectedItem.Text == "Code")
{
string s = "select * from Item_tbl where Code like '" + TextBox1.Text.ToString() + "' +'%' ";
SqlCommand cmd = new SqlCommand(s, cn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
cmd.ExecuteNonQuery();
cn.Close();
}
else if (DropDownList1.SelectedItem.Text == "Description")
{
string s = "select * from Item_tbl where Description like '" + TextBox1.Text.ToString() + "' +'%' ";
SqlCommand cmd = new SqlCommand(s, cn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
cmd.ExecuteNonQuery();
cn.Close();
}
}
catch
{
}
finally
{
cn.Close();
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onmouseover"] = "this.style.cursor = 'hand';";
e.Row.Attributes["onmouseout"] = "this.style.textDecoration = 'none';";
e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (TextBox1.Text == "")
{
TextBox1.Text = GridView1.SelectedRow.Cells[2].Text;
TextBox2.Text = GridView1.SelectedRow.Cells[3].Text;
TextBox3.Text = GridView1.SelectedRow.Cells[4].Text;
}
}