我写了个2个dropdownlist联动,程序能运行起来,但有2个问题:
一 点击第一个dropdownlist1(hylb_type)时候,总是刷新页面;
二 点击第一个dropdownlist1(hylb_type)选择菜单时,第2个dropdownlist2(hylb_name)不跟随着变。第一次打开页面都能正常显示,请大家指点下,是不是哪里有逻辑错误或是别的错误。谢谢!
部分代码如下:
protected override void OnInit(EventArgs e)
{
if (SkinName == null)
ExternalSkinFileName = "qiye.ascx";
else
ExternalSkinFileName = SkinName; base.OnInit(e);
} protected override void OnLoad(EventArgs e)
{
if ( !Page.IsPostBack )
{
DataBind();
}
base.OnLoad( e );
}
protected override void AttachChildControls()
{
hylb_type = (DropDownList)FindControl("hylb_type");
hylb_name = (DropDownList)FindControl("hylb_name");
}public override void DataBind()
{
base.DataBind();
DataSet dshylb_type = new DataSet();
const string cacheKey4 = "hyLB_type";
dshylb_type = myCache.Get(cacheKey4) as DataSet;
if(dshylb_type == null)
{
dshylb_type = SqlHelper.ExecuteDataset(Globals.ConnStr(), CommandType.Text, "SELECT distinct hylb_type FROM T_Hylb");
myCache.Insert(cacheKey4,dshylb_type,3 * myCache.MinuteFactor);
}
hylb_type.DataSource = dshylb_type.Tables[0].DefaultView;
hylb_type.DataTextField = "hylb_type";
hylb_type.DataValueField = "hylb_type";
hylb_type.DataBind();
DataSet ds = new DataSet();
const string cacheKey5 = "ds";
ds = myCache.Get(cacheKey5) as DataSet;
if(ds == null)
{
ds = SqlHelper.ExecuteDataset(Globals.ConnStr(), CommandType.Text, "SELECT hylb_type FROM T_Hylb WHERE hylb = (select hylb from t_corpinfo where corpid ='" + Page.Request.Params["qid"] + "' and flag <> 2)" );
myCache.Insert(cacheKey5,ds,3 * myCache.MinuteFactor);
}
DataSet dshylb_name = new DataSet();
const string cacheKey7 = "hyLB_name";
dshylb_name = myCache.Get(cacheKey7) as DataSet;
if(dshylb_name == null)
{
dshylb_name = SqlHelper.ExecuteDataset(Globals.ConnStr(), CommandType.Text, "SELECT * FROM T_Hylb WHERE hylb_type = '"+ds.Tables[0].Rows[0][0].ToString()+"'");
myCache.Insert(cacheKey7,dshylb_name,3 * myCache.MinuteFactor);
}
if(dshylb_name.Tables[0].Rows.Count > 0)
{
hylb_name.DataSource = dshylb_name.Tables[0].DefaultView;
hylb_name.DataTextField = "hylb_name";
hylb_name.DataValueField = "hylb";
hylb_name.DataBind();
}if(null == Page.Request.Params["qid"] || Page.Request.Params["qid"].ToString() == "")
{
return;
} SqlDataReader dr =
SqlHelper.ExecuteReader(Globals.ConnStr(), CommandType.Text,
"select * from t_corpinfo where corpid ='" + Page.Request.Params["qid"] + "' and flag <> 2" ); if(!dr.HasRows)
{
Page.Response.Redirect(Globals.ApplicationPath+"/msg.aspx?type=delqiye");
} while(dr.Read())
{
if(ds.Tables[0].Rows.Count > 0)
{
hylb_type.SelectedValue = ds.Tables[0].Rows[0][0].ToString();
}
hylb_name.SelectedValue = dr["hylb"].ToString();
}private void InitializeChildControls()
{
hylb_type.SelectedIndexChanged += new System.EventHandler(hylb_type_SelectedIndexChanged);
}private void hylb_type_SelectedIndexChanged(object sender,System.EventArgs e)
{
DataSet dshylb_name = new DataSet();
const string cacheKey6 = "hyLB_name";
dshylb_name = myCache.Get(cacheKey6) as DataSet;
if(dshylb_name == null)
{
dshylb_name = SqlHelper.ExecuteDataset(Globals.ConnStr(), CommandType.Text, "SELECT * FROM T_Hylb WHERE hylb_type = '" + hylb_type.SelectedItem.Text +"' ");
myCache.Insert(cacheKey6,dshylb_name,3 * myCache.MinuteFactor);
}
if(dshylb_name.Tables[0].Rows.Count > 0)
{
hylb_name.DataSource = dshylb_name.Tables[0].DefaultView;
hylb_name.DataTextField = "hylb_name";
hylb_name.DataValueField = "hylb";
hylb_name.DataBind();
}
一 点击第一个dropdownlist1(hylb_type)时候,总是刷新页面;
二 点击第一个dropdownlist1(hylb_type)选择菜单时,第2个dropdownlist2(hylb_name)不跟随着变。第一次打开页面都能正常显示,请大家指点下,是不是哪里有逻辑错误或是别的错误。谢谢!
部分代码如下:
protected override void OnInit(EventArgs e)
{
if (SkinName == null)
ExternalSkinFileName = "qiye.ascx";
else
ExternalSkinFileName = SkinName; base.OnInit(e);
} protected override void OnLoad(EventArgs e)
{
if ( !Page.IsPostBack )
{
DataBind();
}
base.OnLoad( e );
}
protected override void AttachChildControls()
{
hylb_type = (DropDownList)FindControl("hylb_type");
hylb_name = (DropDownList)FindControl("hylb_name");
}public override void DataBind()
{
base.DataBind();
DataSet dshylb_type = new DataSet();
const string cacheKey4 = "hyLB_type";
dshylb_type = myCache.Get(cacheKey4) as DataSet;
if(dshylb_type == null)
{
dshylb_type = SqlHelper.ExecuteDataset(Globals.ConnStr(), CommandType.Text, "SELECT distinct hylb_type FROM T_Hylb");
myCache.Insert(cacheKey4,dshylb_type,3 * myCache.MinuteFactor);
}
hylb_type.DataSource = dshylb_type.Tables[0].DefaultView;
hylb_type.DataTextField = "hylb_type";
hylb_type.DataValueField = "hylb_type";
hylb_type.DataBind();
DataSet ds = new DataSet();
const string cacheKey5 = "ds";
ds = myCache.Get(cacheKey5) as DataSet;
if(ds == null)
{
ds = SqlHelper.ExecuteDataset(Globals.ConnStr(), CommandType.Text, "SELECT hylb_type FROM T_Hylb WHERE hylb = (select hylb from t_corpinfo where corpid ='" + Page.Request.Params["qid"] + "' and flag <> 2)" );
myCache.Insert(cacheKey5,ds,3 * myCache.MinuteFactor);
}
DataSet dshylb_name = new DataSet();
const string cacheKey7 = "hyLB_name";
dshylb_name = myCache.Get(cacheKey7) as DataSet;
if(dshylb_name == null)
{
dshylb_name = SqlHelper.ExecuteDataset(Globals.ConnStr(), CommandType.Text, "SELECT * FROM T_Hylb WHERE hylb_type = '"+ds.Tables[0].Rows[0][0].ToString()+"'");
myCache.Insert(cacheKey7,dshylb_name,3 * myCache.MinuteFactor);
}
if(dshylb_name.Tables[0].Rows.Count > 0)
{
hylb_name.DataSource = dshylb_name.Tables[0].DefaultView;
hylb_name.DataTextField = "hylb_name";
hylb_name.DataValueField = "hylb";
hylb_name.DataBind();
}if(null == Page.Request.Params["qid"] || Page.Request.Params["qid"].ToString() == "")
{
return;
} SqlDataReader dr =
SqlHelper.ExecuteReader(Globals.ConnStr(), CommandType.Text,
"select * from t_corpinfo where corpid ='" + Page.Request.Params["qid"] + "' and flag <> 2" ); if(!dr.HasRows)
{
Page.Response.Redirect(Globals.ApplicationPath+"/msg.aspx?type=delqiye");
} while(dr.Read())
{
if(ds.Tables[0].Rows.Count > 0)
{
hylb_type.SelectedValue = ds.Tables[0].Rows[0][0].ToString();
}
hylb_name.SelectedValue = dr["hylb"].ToString();
}private void InitializeChildControls()
{
hylb_type.SelectedIndexChanged += new System.EventHandler(hylb_type_SelectedIndexChanged);
}private void hylb_type_SelectedIndexChanged(object sender,System.EventArgs e)
{
DataSet dshylb_name = new DataSet();
const string cacheKey6 = "hyLB_name";
dshylb_name = myCache.Get(cacheKey6) as DataSet;
if(dshylb_name == null)
{
dshylb_name = SqlHelper.ExecuteDataset(Globals.ConnStr(), CommandType.Text, "SELECT * FROM T_Hylb WHERE hylb_type = '" + hylb_type.SelectedItem.Text +"' ");
myCache.Insert(cacheKey6,dshylb_name,3 * myCache.MinuteFactor);
}
if(dshylb_name.Tables[0].Rows.Count > 0)
{
hylb_name.DataSource = dshylb_name.Tables[0].DefaultView;
hylb_name.DataTextField = "hylb_name";
hylb_name.DataValueField = "hylb";
hylb_name.DataBind();
}
解决方案 »
- 如何在坐标系中画一个圈
- 如何响应IC卡触发事件
- c#制做水晶報表或RDLC報表,每個報表都要至少要加一個dataset的嗎?
- winform 动态添加节点并将节点保存到xml中?
- 写过Window Service的大神请进
- 关于C# 里面的window server 服务注册的问题
- 在VS内就可以直接对其他某个软件进行操作
- 讨论:研究System.Dianostics命名空间--为windows和web应用程序提供一个安全网的特殊功能
- 简单的字符串转换问题
- 在while循环里给lable动态赋值,显示出lable值得变化。
- 请教一个Graphics类画线的问题。
- 我想使用DataTable进行数据库连接,大虾们能不能给出个思路啊!谢谢啊
<asp:dropdownlist class="selectList" id="hylb_type" runat="server" AutoPostBack = True ></asp:dropdownlist>
<asp:dropdownlist class="selectList" id="hylb_name" runat="server"></asp:dropdownlist>
要使两个连动需要选定dropdownlist1时根据连动条件(如外键)重新绑定dropdownlist2
1 AutoPostBack="True"
2 事件也注册了 hylb_type.SelectedIndexChanged += new System.EventHandler(hylb_type_SelectedIndexChanged);
3 事件也写了private void hylb_type_SelectedIndexChanged(object sender,System.EventArgs e)
{
DataSet dshylb_name = new DataSet();
dshylb_name = SqlHelper.ExecuteDataset(Globals.ConnStr(), CommandType.Text, "SELECT * FROM T_Hylb WHERE hylb_type = '" + hylb_type.SelectedItem.Text +"' ");
if(dshylb_name.Tables[0].Rows.Count > 0)
{
hylb_name.DataSource = dshylb_name.Tables[0].DefaultView;
hylb_name.DataTextField = "hylb_name";
hylb_name.DataValueField = "hylb";
hylb_name.DataBind();
} 4 初始化也绑定数据原了
DataSet dshylb_type = new DataSet();//行业类别
dshylb_type = SqlHelper.ExecuteDataset(Globals.ConnStr(), CommandType.Text, "SELECT distinct hylb_type FROM T_Hylb");
hylb_type.DataSource = dshylb_type.Tables[0].DefaultView;
hylb_type.DataTextField = "hylb_type";
hylb_type.DataValueField = "hylb_type";
hylb_type.DataBind();
DataSet dshylb_name = new DataSet();//行业类别明细
dshylb_name = SqlHelper.ExecuteDataset(Globals.ConnStr(), CommandType.Text, "SELECT * FROM T_Hylb WHERE hylb_type = '"+ds.Tables[0].Rows[0][0].ToString()+"'");if(dshylb_name.Tables[0].Rows.Count > 0)
{
hylb_name.DataSource = dshylb_name.Tables[0].DefaultView;
hylb_name.DataTextField = "hylb_name";
hylb_name.DataValueField = "hylb";
hylb_name.DataBind();
}为什么没有实现联动效果