后台如何调用GridView中的某一个控件

比如某列中我弄了一个Label来显示数据,那么我想在后台protectedvoidPage_Load(objectsender,EventArgse)里面设置Lable.... 比如某列中我弄了一个Label来显示数据,那么我想在后台protected void Page_Load(object sender, EventArgs e)里面设置Lable.text = "第一次显示"
但是发现无法调用到GridView里面的控件,应该怎么才能做到?谢谢~
PS:这是我页面的代码
<asp:TemplateField HeaderText="数量">
<ItemTemplate>
<asp:Label ID="labNum" runat="server" Text="0" Width="32px" Height="20px"></asp:Label>
<asp:TextBox ID="txtNum" runat="server" Width="32px"></asp:TextBox>
<asp:LinkButton ID="lnkbtnEdit" runat="server" CausesValidation="False" CommandName="Edit"
Text="修改" OnClick="lnkbtnEdit_Click" ></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
上面写的太草率了,具体是这样的,我每行后面都有一个【编辑】的按钮,我必须让用户按某一行的【编辑】后,那一行的【单价】那一列里面的Label的text属性发生变化
展开
 我来答
549265480
2009-06-03 · 超过63用户采纳过TA的回答
知道小有建树答主
回答量:286
采纳率:0%
帮助的人:283万
展开全部
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div align="center">

<table width="100%">
<tr><td align="center" bgcolor="#8080FF">
<asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Names="华文隶书"
Font-Size="15pt" Text="部门维护"></asp:Label>
</td></tr>
<tr><td>部门名字:<asp:TextBox ID="TextBox1" runat="server" Width="159px"></asp:TextBox>

<asp:Button ID="Button1" runat="server" Text="添加" onclick="Button1_Click"
Width="67px" />
</td></tr>
<tr><td>
<asp:Button ID="Button2" runat="server" onclick="Button2_Click"
Text="删除选定的记录" />
</td></tr>
<tr><td>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px"
CellPadding="4" Width="90%" onrowcommand="GridView1_RowCommand"
onrowdatabound="GridView1_RowDataBound">
<RowStyle BackColor="White" ForeColor="#003399" />
<Columns>
<asp:TemplateField HeaderText="选择">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
<HeaderTemplate>
<asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="True"
oncheckedchanged="CheckBox2_CheckedChanged" Text="全选" />
</HeaderTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="自动编号">
<ItemStyle Width="15%" />
</asp:BoundField>
<asp:BoundField DataField="departmentID" HeaderText="部门编号" >
<ItemStyle Width="15%" />
</asp:BoundField>
<asp:BoundField DataField="departmentName" HeaderText="部门名字" >
<ItemStyle Width="30%" />
</asp:BoundField>
<asp:TemplateField HeaderText="操作">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server"
CommandArgument='<%# Eval("departmentID") %>' CommandName="edit1">编辑</asp:LinkButton>

<asp:LinkButton ID="LinkButton2" runat="server"
CommandArgument='<%# Eval("departmentID") %>' CommandName="delete1">删除</asp:LinkButton>
</ItemTemplate>
<ItemStyle Width="30%" />
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
<PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
<SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
<HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
</asp:GridView>
</td></tr>
</table>
</div>
</form>
</body>
</html>

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

//
using System.Data.SqlClient;

public partial class bumen : System.Web.UI.Page
{

private void DataBindData()
{
String cnnstr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
SqlConnection cnn = new SqlConnection(cnnstr);
SqlCommand cmd = new SqlCommand("select * from department", cnn);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();

}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataBindData();
}

}
protected void Button1_Click(object sender, EventArgs e)
{
String cnnstr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
SqlConnection cnn = new SqlConnection(cnnstr);
String cmdstr = "";
SqlCommand cmd = new SqlCommand();
cmdstr = "insert into department(departmentName) values(@departmentName)";
cmd.CommandText = cmdstr;

SqlParameter p = new SqlParameter("@departmentName", SqlDbType.VarChar, 50);
p.Value = TextBox1.Text;

cmd.Parameters.Add(p);

try
{
cnn.Open();
cmd.Connection = cnn;
cmd.ExecuteNonQuery();
DataBindData();
}
catch (Exception ex)
{
throw ex;
}
finally
{
cnn.Close();
}

}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
switch (e.CommandName)
{
case "delete1":

String cnnstr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
SqlConnection cnn = new SqlConnection(cnnstr);
String cmdstr = "";
SqlCommand cmd = new SqlCommand();
cmdstr = "delete from department where departmentID=@DepartmentID";
cmd.CommandText = cmdstr;

SqlParameter p = new SqlParameter("@DepartmentID", SqlDbType.Int);
p.Value = e.CommandArgument;

cmd.Parameters.Add(p);

try
{
cnn.Open();
cmd.Connection = cnn;
cmd.ExecuteNonQuery();
DataBindData();
}
catch (Exception ex)
{
throw ex;
}
finally
{
cnn.Close();
}
break;
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

//生成自动编号列
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[1].Text = (e.Row.RowIndex + 1).ToString();
}

//如果是绑定数据行
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
{
LinkButton lk = (LinkButton)e.Row.Cells[4].FindControl("LinkButton2");
if (lk != null)
lk.Attributes.Add("onclick", "javascript:return confirm('你确认要删除:\"" + e.Row.Cells[3].Text + "\"吗?')");
//((LinkButton)e.Row.Cells[2].Controls[1]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除:\"" + e.Row.Cells[1].Text + "\"吗?')");
}
}
}
protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
{
CheckBox cb2 = (CheckBox)(sender);
if (cb2.Checked)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox cb1 = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if (cb1 != null && !cb1.Checked)
{
cb1.Checked = true;
}
}
}
else
{

for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox cb1 = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if (cb1 != null && cb1.Checked)
{
cb1.Checked = false ;
}
}

}
}
protected void Button2_Click(object sender, EventArgs e)
{
String cnnstr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
SqlConnection cnn = new SqlConnection(cnnstr);
String cmdstr = "";
SqlCommand cmd = new SqlCommand();
try
{
cnn.Open();
cmd.Connection = cnn;
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
CheckBox cb = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if (cb!=null && cb.Checked)
{
String idd = GridView1.Rows[i].Cells[2].Text;
cmdstr = "delete from department where departmentID= " + idd;
cmd.CommandText = cmdstr;
cmd.ExecuteNonQuery();
}
}

DataBindData();
}
catch (Exception ex)
{
throw ex;
}
finally
{
cnn.Close();
}
}
}

这个 比较完整 数据库 没有 给你 你自己看着办吧 呵呵
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
allrise333
2009-06-03 · 超过19用户采纳过TA的回答
知道答主
回答量:42
采纳率:0%
帮助的人:55.2万
展开全部
Label lable = gridView.Rows[i].FindControl("Label的ID") as Label;
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式