从gridview中选择并插入到Textbox中 [英] Selection from gridview and insert in Textbox
本文介绍了从gridview中选择并插入到Textbox中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,我在设计中有一个面板,下拉列表,文本框和gridview.
我在文本框中有搜索代码.当我在该文本框中输入任何文本时,我会得到该
导致gridview.在那个gridview中显示结果后,当我单击任何
该特定行的数据,我需要在表中获取该数据.
(表包含3个文本框).
这是代码. .
表格:
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>
在cs文件中:
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)
{
//on selecting the results, I have written this below code, but when I have many display results, I am getting only one row i.e., starting row but not a selected row
if (TextBox1.Text == "")
{
TextBox1.Text = GridView1.SelectedRow.Cells[2].Text;
TextBox2.Text = GridView1.SelectedRow.Cells[3].Text;
TextBox3.Text = GridView1.SelectedRow.Cells[4].Text;
}
}
推荐答案
" + e.Row.RowIndex); } } 受保护的 无效 GridView1_SelectedIndexChanged(对象发件人,EventArgs e) { // 在选择结果时,我已将此代码写在下面的代码中,但是当我有很多显示结果时,我只会得到一行,即起始行而不是选定行 如果(TextBox1.Text == " ) { TextBox1.Text = GridView1.SelectedRow.Cells [ 2 ].Text; TextBox2.Text = GridView1.SelectedRow.Cells [ 3 ].Text; TextBox3.Text = GridView1.SelectedRow.Cells [ 4 ].Text; } }
" + e.Row.RowIndex); } } protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) { //on selecting the results, I have written this below code, but when I have many display results, I am getting only one row i.e., starting row but not a selected row if (TextBox1.Text == "") { TextBox1.Text = GridView1.SelectedRow.Cells[2].Text; TextBox2.Text = GridView1.SelectedRow.Cells[3].Text; TextBox3.Text = GridView1.SelectedRow.Cells[4].Text; } }
void CustomersGridView_SelectedIndexChanging(Object sender, GridViewSelectEventArgs e)
{
GridViewRow row = CustomersGridView.Rows[e.NewSelectedIndex];
// You c an cancel the select operation by using the Cancel// property. For this example, if the user selects a customer with // the ID "ANATR", the select operation is canceled and an error message// is displayed.if (row.Cells[1].Text == "ANATR")
{
e.Cancel = true;
MessageLabel.Text = "You cannot select " + row.Cells[2].Text + ".";
}
}
这篇关于从gridview中选择并插入到Textbox中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文