请教在ascx中引用DropDownList但是返回值总为0的烦人的问题(在线等待)
一直困扰的问题:
在ascx中生成一个DropDownList,然后在aspx模板列中引用该控件,想要实现的是:
当点击某行的编辑按钮时让该DropDownList显示的值就等于该行中的某一字段值。
但现在问题是:catid返回的值总是为零???所以该DropDownList每次显示的还是第一项的值。
请各位大侠帮忙指点!!!
源代码如下:
1、cat.ascx.cs:
public abstract class cat : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.DropDownList DropDownList1;
public string ConnString;
protected int _catid=0;
public int catid
{
get
{
return Convert.ToInt32(DropDownList1.SelectedItem.Value);
}
set
{
_catid=value;
Trace.Write("set catid", _catid.ToString());
try
{
DropDownList1.Items.FindByValue(_catid.ToString()).Selected=true;
}
catch {}
}
}
void LoadData()
{
SqlDataAdapter mycommand = new SqlDataAdapter("Select * From tb_x",ConnString);
DataSet ds = new DataSet();
mycommand.Fill(ds,"tb_x");
DataView dv = new DataView(ds.Tables[0]);
DropDownList1.DataSource = dv;
DropDownList1.DataValueField = "xid";
DropDownList1.DataTextField = "xmc";
DropDownList1.DataBind();
}
private void Page_Load(object sender, System.EventArgs e)
{
IniFile ini = new IniFile("C:\\set.ini");
ConnString = ini.IniReadValue("Connection","connstring");
if (ViewState["DataLoaded"] == null)
{
LoadData();
ViewState["DataLoaded"] = true;
}
}
2、test.aspx:
<%@ Register TagPrefix="uc1" TagName="cat" Src="cat.ascx" %>
<asp:DataGrid id="DataGrid1" runat="server" OnUpdateView="UpdateView" AutoGenerateColumns="false">
<Columns>
<ASP:BOUNDCOLUMN runat="server" headertext="系名称" datafield="xmc"></ASP:BOUNDCOLUMN>
<ASP:TEMPLATECOLUMN HeaderText="系名称">
<EditItemTemplate>
<uc1:cat id=Cat1 runat="server" catid='<%# int.Parse(DataBinder.Eval(Container.DataItem, "xid").ToString())%>'>
</uc1:cat>
</EditItemTemplate>
</ASP:TEMPLATECOLUMN>
</COLUMNS>
</DataGrid>
谢谢大家!!!