asp.net GridVIew 编辑时候获取非编辑状态下控件的内容

页面中有一GridView,使用自带的编辑功能,点击编辑的时候,其中一个模板列的编辑状态是个DataList绑定数据没问题,就是设置SelectVlaue的时候老是空指针... 页面中有一GridView, 使用自带的编辑功能,点击编辑的时候,其中一个模板列的 编辑状态 是个DataList 绑定数据没问题,就是设置 SelectVlaue的时候 老是 空指针异常 我用的是((DropDownList)(GridViewUserList.Rows[inde].Cells[3].FindControl("DropDownListRoles"))).SelectedValue = ((Label)(GridViewUserList.Rows[inde].Cells[3].FindControl("Label3"))).Text; 其中inde 是当前编辑行的索引, 有没有好的办法能获取 GridVIew 在非编辑状态下显示的值
模板列 本来是文本也就是Label 编辑状态下,我用的DataList 用GridView.Rows[inde].Cells[3] 的形式获取的值为"" 空.
展开
 我来答
549265480
2009-07-04 · 超过63用户采纳过TA的回答
知道小有建树答主
回答量:286
采纳率:0%
帮助的人:279万
展开全部
你说的是datalist嵌套吧 我举个例子 你看下 行不行

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:DataList ID="DataList1" runat="server" Width="463px"
onitemdatabound="DataList1_ItemDataBound">
<ItemTemplate>
<a href='newsList.aspx?id=<%# Eval("newstypeid")%>'><%#Eval("newstypeName") %></a> 
<br />

<asp:DataList ID="DataList2" runat="server">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl="~/IMAGES/fw.gif" />
 
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl='<%# Eval("newsID","newsdetails.aspx?id={0}") %>'
Text='<%# Eval("newsTitle") %>'></asp:HyperLink>
 
<asp:Label ID="Label1" runat="server" Text='<%# Eval("newsdate","{0:d}") %>'></asp:Label>
</ItemTemplate>
</asp:DataList>
</ItemTemplate>
</asp:DataList>

</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;
using DAL;

public partial class NewClassDisplay : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
yasbind();
}

}

protected void yasbind()
{
//DataSet ds = new DataSet();
//String cnnstr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
//SqlConnection cnn = new SqlConnection(cnnstr);
//String cmdstr = "";
//SqlCommand cmd = new SqlCommand();
String cmdstr = "select * from newstype";
//cmd.CommandText = cmdstr;
//cmd.Connection = cnn;
//SqlDataAdapter sda = new SqlDataAdapter(cmd);

try
{

//sda.Fill(ds);

//DataList1.DataSource = ds.Tables[0].DefaultView;
//DataList1.DataBind();

//DataList2.DataSource = ds.Tables[1].DefaultView;
//DataList2.DataBind();

// DataList1.DataSource = ds.Tables[0].DefaultView;
Dal yy = new Dal();

DataList1.DataSource = yy.dal_DS(cmdstr);
DataList1.DataBind();

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

protected void yasbind_s(string sql,DataList dl)
{
DataSet ds = new DataSet();
String cnnstr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
SqlConnection cnn = new SqlConnection(cnnstr);
String cmdstr = "";
SqlCommand cmd = new SqlCommand();
cmdstr = sql;
cmd.CommandText = cmdstr;
cmd.Connection = cnn;
SqlDataAdapter sda = new SqlDataAdapter(cmd);

try
{

sda.Fill(ds);

//DataList1.DataSource = ds.Tables[0].DefaultView;
//DataList1.DataBind();

//DataList2.DataSource = ds.Tables[1].DefaultView;
//DataList2.DataBind();

dl.DataSource = ds.Tables[0].DefaultView;
dl.DataBind();

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

protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
// e.Item.FindControl(
DataRowView rowv = (DataRowView)e.Item.DataItem; //一定知道该代码的含义?
String newstypeid = rowv["newstypeid"].ToString();
string sql = "select top 10 newsid, newstitle,newsdate from news where newstypeid="+newstypeid+" order by newsid ";
DataList dl = (DataList)e.Item.FindControl("DataList2");
if (dl!=null)
yasbind_s(sql, dl);

}
}
_w_j_f_
2009-07-05 · TA获得超过225个赞
知道答主
回答量:174
采纳率:0%
帮助的人:103万
展开全部
自带的编辑功能不好,建议自己做编辑页面,个人认为比用自带的编辑功能还要简单点,而且出了错也更容易快速找到
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
yingwuzong1989
2009-07-01 · 超过65用户采纳过TA的回答
知道小有建树答主
回答量:154
采纳率:0%
帮助的人:93.1万
展开全部
((DropDownList)(GridViewUserList.Rows[inde].Cells[3].FindControl("DropDownListRoles"))).SelectedItem.Text = ((Label)(GridViewUserList.Rows[inde].Cells[3].FindControl("Label3"))).Text;
你试下这条代码,driodownlist的选择项应该是selecteditem,不应该是selectvalue
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式