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

2024-12-18 14:08:47
推荐回答(3个)
回答1:

你说的是datalist嵌套吧 我举个例子 你看下 行不行



无标题页





onitemdatabound="DataList1_ItemDataBound">

'><%#Eval("newstypeName") %> 






 
NavigateUrl='<%# Eval("newsID","newsdetails.aspx?id={0}") %>'
Text='<%# Eval("newsTitle") %>'>

 
'>










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);

}
}

回答2:

自带的编辑功能不好,建议自己做编辑页面,个人认为比用自带的编辑功能还要简单点,而且出了错也更容易快速找到

回答3:

((DropDownList)(GridViewUserList.Rows[inde].Cells[3].FindControl("DropDownListRoles"))).SelectedItem.Text = ((Label)(GridViewUserList.Rows[inde].Cells[3].FindControl("Label3"))).Text;
你试下这条代码,driodownlist的选择项应该是selecteditem,不应该是selectvalue