这是查询后gridview结果,我想点击载入,把每列数据分别载入不同的textbox中。求实现代码
3个回答
展开全部
1:首先,你这个按钮,是GRIDVIEW自已AUTOGENERATION生成的控件吗?如果是,就当它是自带的选选择行的控件
就这样:在SelectedIndexChanged事件里面,得到选 中行的索引,int index = gridview1.SelectedIndex,
TextBox1.Text = gridview1.Rows[rowIndex].Cells[你要选取的值].Text;
2:如果你这个按钮是自己添加的模版 ,在编辑模版列双击按钮,生成事件,里面这样写
Button btn = sender as Button;
用int rowIndex = (btn.NamingContainer as GridViewRow).RowIndex;
gridview1.selectedIndex = rowIndex;
TextBox1.Text = gridview1.Rows[rowIndex].Cells[你要选取的值].Text;
3:如果想客户端交互感觉好些,模版上的按钮可以用客户端控件,就是INPUT的按钮,写客户端事件
这样不会有刷新,函数这样写,单击事件onclick=" return fillText(this.parentElement.parentElement)"
<script>function fillText(row)
{
var txt = row.cells[你要填的单元格序号].children[0].value;//如果.value取不到值,可以改innerText
var txtbox = document.getElementById('<%=TextBox1.ClientID%>');
txtbox.value = txt;
后面的照前面套,这样的不用刷新
}
</script>
就这样:在SelectedIndexChanged事件里面,得到选 中行的索引,int index = gridview1.SelectedIndex,
TextBox1.Text = gridview1.Rows[rowIndex].Cells[你要选取的值].Text;
2:如果你这个按钮是自己添加的模版 ,在编辑模版列双击按钮,生成事件,里面这样写
Button btn = sender as Button;
用int rowIndex = (btn.NamingContainer as GridViewRow).RowIndex;
gridview1.selectedIndex = rowIndex;
TextBox1.Text = gridview1.Rows[rowIndex].Cells[你要选取的值].Text;
3:如果想客户端交互感觉好些,模版上的按钮可以用客户端控件,就是INPUT的按钮,写客户端事件
这样不会有刷新,函数这样写,单击事件onclick=" return fillText(this.parentElement.parentElement)"
<script>function fillText(row)
{
var txt = row.cells[你要填的单元格序号].children[0].value;//如果.value取不到值,可以改innerText
var txtbox = document.getElementById('<%=TextBox1.ClientID%>');
txtbox.value = txt;
后面的照前面套,这样的不用刷新
}
</script>
展开全部
很简单的 前台你的gridview
asp:TemplateField HeaderText="name" >
<ItemTemplate>
<asp:Label runat="server" ID="lbname" Text='<%# Eval("name") >'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
这样绑定数据,然后后台在你的selectIndexChanged事件里面
Lable lblNmae= gridview.rows[selectIndex].findControl("lbname") as Lable;
string strName=lblName.text;//这里就是要的数据了,其他仿照这样应该没问题。
asp:TemplateField HeaderText="name" >
<ItemTemplate>
<asp:Label runat="server" ID="lbname" Text='<%# Eval("name") >'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
这样绑定数据,然后后台在你的selectIndexChanged事件里面
Lable lblNmae= gridview.rows[selectIndex].findControl("lbname") as Lable;
string strName=lblName.text;//这里就是要的数据了,其他仿照这样应该没问题。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这个你要加个事件啊,正好我做过,代码还在,前台加2个隐藏控件:
<asp:TextBox ID="TextBox10" runat="server" Width="0px" BorderWidth="0px"
Height="0px" BackColor="#EFF7FF"></asp:TextBox>
<asp:Button ID="Button2" runat="server" onclick="Button2_Click"
BorderWidth="0px" Height="0px" Width="0px" />
<script type="text/javascript">
function ClickEvent1(r1)
{
document.getElementById("<%= TextBox10.ClientID %>").value = r1;
document.getElementById("<%= Button2.ClientID %>").click();
}
</script>
后台:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", " c =this.style.backgroundColor; this.style.backgroundColor='#96C2F1'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor = c");
e.Row.Attributes["style"] = "cursor:hand";
e.Row.Attributes.Add("OnClick", "ClickEvent1('" + e.Row.Cells[0].Text + "','" + e.Row.Cells[2].Text + "')");
}
}
protected void Button2_Click(object sender, EventArgs e)
{
SqlConnection sqlcon = new SqlConnection();
sqlcon.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DB_CON"].ConnectionString;
sqlcon.Open();
SqlCommand sqlcom = new SqlCommand();
sqlcom.Connection = sqlcon;
sqlcom.CommandText = "select * from weather where CMD_ID = '" + TextBox10.Text + "' and Timestamp = '" + TextBox12.Text + "'";
sqlcom.CommandType = CommandType.Text;
SqlDataAdapter sqlda = new SqlDataAdapter(sqlcom);
ds = new DataSet();
sqlda.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
TextBox1.Text = ds.Tables[0].Rows[0][0].ToString();
TextBox2.Text = ds.Tables[0].Rows[0][8].ToString();
...
}
<asp:TextBox ID="TextBox10" runat="server" Width="0px" BorderWidth="0px"
Height="0px" BackColor="#EFF7FF"></asp:TextBox>
<asp:Button ID="Button2" runat="server" onclick="Button2_Click"
BorderWidth="0px" Height="0px" Width="0px" />
<script type="text/javascript">
function ClickEvent1(r1)
{
document.getElementById("<%= TextBox10.ClientID %>").value = r1;
document.getElementById("<%= Button2.ClientID %>").click();
}
</script>
后台:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", " c =this.style.backgroundColor; this.style.backgroundColor='#96C2F1'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor = c");
e.Row.Attributes["style"] = "cursor:hand";
e.Row.Attributes.Add("OnClick", "ClickEvent1('" + e.Row.Cells[0].Text + "','" + e.Row.Cells[2].Text + "')");
}
}
protected void Button2_Click(object sender, EventArgs e)
{
SqlConnection sqlcon = new SqlConnection();
sqlcon.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DB_CON"].ConnectionString;
sqlcon.Open();
SqlCommand sqlcom = new SqlCommand();
sqlcom.Connection = sqlcon;
sqlcom.CommandText = "select * from weather where CMD_ID = '" + TextBox10.Text + "' and Timestamp = '" + TextBox12.Text + "'";
sqlcom.CommandType = CommandType.Text;
SqlDataAdapter sqlda = new SqlDataAdapter(sqlcom);
ds = new DataSet();
sqlda.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
TextBox1.Text = ds.Tables[0].Rows[0][0].ToString();
TextBox2.Text = ds.Tables[0].Rows[0][8].ToString();
...
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询