在.NET将aspx页面中的无页面刷新二级联动移植到ascx控件中

今天准备做一个无页面刷新的控件,原本以为只需要将其代码直接从页面中转过来就行了,没有想到,转过来之后居然只有DropDownList1里有值, DropDownList2空白.

百思不得其解,后来想想,是不是JavaScript里的接收值ID[DropDownList2]没有接收到值或者是DropDownList2无效呢?

运行之后查看源文件,果然,他的ID变成了"Zz1_DropDownList2"了,这可能是与注册控件有关吧.

.NET中直接拖放到页面中的控件,系统默认将其控件名称修改为:"控件名" + "1" + "DropDownList2"

改过来之后,OK.

zz.ascx:

 

using  System;
using  System.Data;
using  System.Configuration;
using  System.Collections;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;

public   partial   class  zz : System.Web.UI.UserControl
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        DropDownList2.Attributes.Add(
"onBlur""" + this.myHidden.ClientID + ".value=this.options[this.selectedIndex].value");
        
if (!this.IsPostBack)
        
{
            
this.down1_bind();
            
this.DropDownList1.Attributes.Add("onchange""XmlPost2(this);");
        }

        
if (shopClassFatherId != "")
        
{
            
this.down2_bind(shopClassFatherId);
        }

    }


    GetData GD 
= new GetData();
    DataSet ds 
= new DataSet();

    
private string shopClassFatherId
    
{
        
get
        
{
            
if (ViewState["shopClassFatherId"!= null && ViewState["shopClassFatherId"].ToString() != "")
            
{
                
return ViewState["shopClassFatherId"].ToString();
            }

            
else
            
{
                
if (Request["shopClassFatherId"!= null && Request["shopClassFatherId"].ToString() != "")
                
{
                    
return Request["shopClassFatherId"];
                }

                
else
                
{
                    
return "";
                }

            }

        }

        
set
        
{
            ViewState[
"shopClassFatherId"= value;
        }

    }

    
private string shop_classid
    
{
        
get
        
{
            
if (ViewState["shop_classid"!= null && ViewState["shop_classid"].ToString() != "")
            
{
                
return ViewState["shop_classid"].ToString();
            }

            
else
            
{
                
if (Request["shop_classid"!= null && Request["shop_classid"].ToString() != "")
                
{
                    
return Request["shop_classid"];
                }

                
else
                
{
                    
return "";
                }

            }

        }

        
set
        
{
            ViewState[
"shop_classid"= value;
        }

    }

    
private void down1_bind()
    
{
        
string sql = "select shopClassFatherId,shopClassFatherName from shopClassFather order by shopClassFatherId";
        DataSet ds 
= GD.DataBind(sql);
        
this.DropDownList1.DataSource = ds;
        
this.DropDownList1.DataValueField = "shopClassFatherId";
        
this.DropDownList1.DataTextField = "shopClassFatherName";
        
this.DropDownList1.DataBind();
    }


    
private void down2_bind(string id)
    
{
        
string mystr = "";
        
string sql = "select shop_classid,name from shopclass where father = '" + id + "' order by shop_classid";
        DataSet ds 
= GD.DataBind(sql);

        
if (ds.Tables[0].Rows.Count != 0)
        
{
            
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            
{
                mystr 
+= "," + ds.Tables[0].Rows[i][0].ToString() + "|" + ds.Tables[0].Rows[i][1].ToString();
            }

            mystr 
= mystr.Substring(1);
        }

        
this.Response.Write(mystr);
        
this.Response.End();
    }


    
int fatherId;
    
int firstClassId;
    
string content;
    
protected void Button1_Click(object sender, EventArgs e)
    
{
        fatherId 
= Convert.ToInt32(DropDownList1.Text.ToString());
        
if (this.myHidden.Value == "" || Convert.ToInt32(this.myHidden.Value.ToString()) == 0)
        
{
            
return;
        }

        
else
        
{
            firstClassId 
= Convert.ToInt32(this.myHidden.Value); 
        }

        content 
= TextBox1.Text.ToString();
        
if (fatherId == 1)
        
{
            Response.Redirect(
"SearchProduct.aspx?firstClassId=" + firstClassId + "&key=" + content);
        }

        
if (fatherId == 2)
        
{
            Response.Redirect(
"SearchShop.aspx?firstClassId=" + firstClassId + "&key=" + content);
        }

        
if (fatherId == 3)
        
{
            Response.Redirect(
"Searchshopkeeper.aspx?firstClassId=" + firstClassId + "&key=" + content);
        }

    }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值