datagrid里的值,我怎么才能出来?

ouisn 2004-01-07 04:34:01
datagrid里的值,我怎么才能出来?假若,我在datagrid的一列中加入一个控件
label我怎么才能取到label.text?
...全文
39 28 打赏 收藏 转发到动态 举报
写回复
用AI写文章
28 条回复
切换为时间正序
请发表友善的回复…
发表回复
happer6012 2004-01-07
  • 打赏
  • 举报
回复
e.Item.FindControl("Label1")
xtmyname 2004-01-07
  • 打赏
  • 举报
回复
Label la=e.Item.FindControl("Label1");
la.Text="aaa";
ouisn 2004-01-07
  • 打赏
  • 举报
回复
acewang(平平安安过一年) 非常感谢你的指导,嘿嘿,其实我的问题是这样的
我用的是table,我的table 是
'name :set_TableRow
'功能 :table的表示数据
'作者 :oui
'参数1:table 设计的table ID
'参数2: arrStr()() 数组--传入数据(cell(0).text,cell(0).link,cell(1).text)
Function set_TableRow(ByVal table As Table, ByVal arrStr()() As String)
Dim r As TableRow
Dim c As TableCell
Dim h As HyperLink
Dim i As Integer
For i = 0 To arrStr.Length - 1
r = New TableRow()
c = New TableCell()
set_TableCell(c, r, arrStr(i)(0), "0", arrStr(i)(1))
c = New TableCell()
set_TableCell(c, r, arrStr(i)(2), "1", "")
table.Rows.Add(r)
Next i
End Function
'name :set_TableCell
'功能 :table.cell的表示数据
'作者 :oui
'参数1:cell TableCell
'参数2: row TableRow
'参数3: strText cell(0).text or cell(1).text
'参数4: strS '0'strText为cell(0).text,其他为cell(1).text
'参数5: strUrl cell(0).link
Function set_TableCell(ByVal cell As TableCell, ByVal row As TableRow, ByVal strText As String, ByVal strS As String, ByVal strUrl As String)
Dim h As New HyperLink()
cell.BorderStyle = BorderStyle.Solid
cell.BorderStyle = BorderStyle.Solid
cell.BorderWidth = Unit.Point(1)
If strS = 0 Then
h.Text = strText
h.Font.Bold = True
h.NavigateUrl = strUrl
cell.Controls.Add(h)
Else
cell.Text = strText
End If
row.Cells.Add(cell)
End Function
是这样做的,我现在想取cell中的HyperLink的text.哈哈,帮帮忙了
acewang 2004-01-07
  • 打赏
  • 举报
回复
需要适当的修改,比如table至少是runat的,行换成HtmlTableRow或TableRow(服务器控件)
ouisn 2004-01-07
  • 打赏
  • 举报
回复
对table这个控件也是一样的吧
acewang 2004-01-07
  • 打赏
  • 举报
回复
Cells[0]改为Cells(0), c#写习惯了:-(
0可对应改为你的Label所在的列
acewang 2004-01-07
  • 打赏
  • 举报
回复
Dim i as DataGridItem
For Each i In DataGrid1.Items
if (i.ItemType=ListItemType.Item or i.ItemType=ListItemType.AlternatingItem) then
Dim L as Label=Ctype(i.Item.Cells[0].FindContrl("Label1"),Label)
if Not L is Nothing Then
Reponse.Write(L.Text)
End if
End if
Next
ZHANG9652 2004-01-07
  • 打赏
  • 举报
回复
方法很多,楼上的那些朋友已经说过了
DimVar 2004-01-07
  • 打赏
  • 举报
回复
Label lb = CType(e.Item.FindControl("myLabel"), Label);
liuvb 2004-01-07
  • 打赏
  • 举报
回复
转版主的
保存为.aspx文件,然后打开看一下.
<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="gb2312" %>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<script language="C#" runat="server">
DataSet ds;
void Page_Load(Object sender, EventArgs e)
{
BindGrid();
}
void BindGrid() {
SqlConnection myConnection = new SqlConnection("server=(local)\\NetSDK;database=pubs;Integrated Security=SSPI");
SqlDataAdapter myCommand = new SqlDataAdapter("select * from Authors", myConnection);
ds = new DataSet();
myCommand.Fill(ds, "Authors");
MyDataGrid.DataSource=ds.Tables["Authors"].DefaultView;
MyDataGrid.DataBind();
if(Session["chk"]!=null)
l.Text = (string)Session["chk"];
else
l.Text="";
}
void sb(Object sender, EventArgs E) {
string a = "";
for( int i = 0; i < MyDataGrid.Items.Count; i++ ){
string uid = MyDataGrid.Items[i].UniqueID+":" ;
string a1="";
if(Request.Form[uid+"Check1"]==null)
{
a1 = "close";
}
else{
a1 = Request.Form[uid+"Check1"].ToString();
}
string a2 = MyDataGrid.Items[i].Cells[1].Text;
string a3 = Request.Form[uid+"rblIndexShow"].ToString();
string a4 = "";
Label l;
l = (Label)MyDataGrid.Items[i].FindControl("lb");
if(!(l == null) )
a4 = l.Text;
string a5 = "";
TextBox t;
t = (TextBox)MyDataGrid.Items[i].FindControl("tb");
if(!(t == null) )
a5 = t.Text;
a += a1+"  "+a2+"  "+a3+"  "+a4+"  "+a5+"<br>";
}
Session["chk"] = a;
BindGrid();
}
</script>
<script language="JavaScript">
function tt1()
{
var spans = MyDataGrid.all.tags("SPAN");
for (var i=0; i < spans.length; i++)
{
alert(spans[i].innerText)
}
}
function tt2()
{
var spans = MyDataGrid.all.tags("INPUT");
for (var i=0; i < spans.length; i++)
{
if(spans[i].type =="text")
alert(spans[i].value)
}
}
</script>
<body>

<h3><font face="Verdana">Simple Select to a DataGrid Control</font></h3>
<form runat="server">

<ASP:DataGrid id="MyDataGrid" runat="server"
AutoGenerateColumns="false"
Width="700"
BackColor="#ccccff"
BorderColor="black"
ShowFooter="false"
CellPadding=3
CellSpacing="0"
Font-Name="Verdana"
Font-Size="8pt"
HeaderStyle-BackColor="#aaaadd"
EnableViewState="false">
<columns>
<asp:templatecolumn>
<itemtemplate>
<asp:checkbox ID="Check1" runat="server"/></itemtemplate></asp:templatecolumn>
<asp:boundcolumn DataField="au_lname" HeaderText="au_lll"></asp:boundcolumn>
<asp:templatecolumn>
<itemtemplate>
<asp:RadioButtonList id="rblIndexShow" Enabled="True" runat="server"
CellPadding="0" RepeatDirection="Horizontal" CellSpacing="0" DataValueField="FirstShowMenu">
<asp:ListItem Value="1">是</asp:ListItem>
<asp:ListItem Value="2" Selected="True">否</asp:ListItem>
</asp:RadioButtonList>
</itemtemplate></asp:templatecolumn>
<asp:templatecolumn><headertemplate>Label</headertemplate>
<itemtemplate>
<asp:Label ID="lb" Text='<%# DataBinder.Eval(Container.DataItem, "au_lname") %>' Runat="server"/>
</itemtemplate></asp:templatecolumn>
<asp:templatecolumn><headertemplate>TextBox</headertemplate>
<itemtemplate>
<asp:TextBox ID="tb" Text='<%# DataBinder.Eval(Container.DataItem, "au_lname") %>' Runat="server"/>
</itemtemplate></asp:templatecolumn>
</columns>
</ASP:DataGrid>
<asp:button ID="bt" OnClick="sb" Text="服务器端遍历整个DataGrid" runat="server"/><br>
<asp:label ID="l" Text="" runat="server"></asp:label><br>
<asp:label ID="lb" runat="server"></asp:label>
<asp:placeholder ID="pc" runat="server"/>
<INPUT type="submit" value="客户端遍历Label" onclick="tt1()"><br>
<INPUT type="submit" value="客户端遍历TextBox" onclick="tt2()">
</form>
<a href="http://218.84.107.5/jj.rar">下载源码!</a>
</body>
</html>
py3zhsh 2004-01-07
  • 打赏
  • 举报
回复
學習
ouisn 2004-01-07
  • 打赏
  • 举报
回复
acewang 2004-01-07
  • 打赏
  • 举报
回复
全部遍历?
ouisn 2004-01-07
  • 打赏
  • 举报
回复
对,按钮是外面附加的,不是datagrid的
ouisn 2004-01-07
  • 打赏
  • 举报
回复
vb中不允许(Label)强制转换
acewang 2004-01-07
  • 打赏
  • 举报
回复
按钮是在DataGrid外面对吧? DataGrid有没有处于什么状态,比如编辑
yanyl2001 2004-01-07
  • 打赏
  • 举报
回复

string labelText = ((Label)(GridEdit.Items[1].Cells[2].FindControl("label"))).Text;
ouisn 2004-01-07
  • 打赏
  • 举报
回复
hoge66 不好用啊 For Each DataGridItem1 In DataGrid1.Items 有错

Me.TextBox1.Text = Table1.Rows.FindControl("HyperLink")有错
hoge66 2004-01-07
  • 打赏
  • 举报
回复
For Each DataGridItem1 In DataGrid1.Items
label1.text = DataGridItem1.FindControl("chkExport") 'chkExport是checkBox的ID
Next
ouisn 2004-01-07
  • 打赏
  • 举报
回复
用vb.net好吗
加载更多回复(8)

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧