从客户端获取ASP下拉列表的ID

Getting the ID in of an ASP drop down list from client side

本文关键字:ID 下拉列表 ASP 客户端 获取      更新时间:2023-09-26

我要做的是让我的下拉列表之一改变其内容每当选择的项目在另一个变化。我在我的aspx文件中有以下代码:

function ModifyDDLItems(id1, id2) 
{
    var ddlcontrolShown = document.getElementById(id1);
    var ddlcontrolHidden = document.getElementById(id2);
    if (ddlcontrolShown.options[ddlcontrolShown.selectedIndex].value == "DD1") 
    {
        //Get number of items of hidden ddl
        var length = ddlcontrolHidden.options.length;
        //Clear items of shown ddl
        ddlcontrolShown.options.length = 0;
        //Add itmems of hidden ddl to shown ddl
        for (i = 0; i < length; i++) 
        {
            ddlcontrolShown.options.add
            var newoption = document.createElement("option")
            newoption.text = ddlcontrolHidden.options[i].text;
            newoption.value = ddlcontrolHidden.options[i].text.value;
        }         
    }   
}

现在,我给它的前端ID通过这个:

protected void SetDD1ConfItems(GridViewRow gvRow, DataSet BaseConfItems)
{
    DataView dvConfType = new DataView(BaseConfItems.Tables[0]);
    DataSet dsTemp = BaseConfItems.Clone();
    DropDownList ddlConfType2 = (DropDownList)form1.FindControl("ddlConfType2");
    DropDownList ddlBA = (DropDownList)gvRow.FindControl("ddlBA");
    DropDownList ddlConfType = (DropDownList)gvRow.FindControl("ddlConfType");
    dvConfType.RowFilter = "ref_code = 'FAX' or ref_code = 'EEX' or ref_code = 'EPD'";
    dsTemp.Tables.Clear();
    dsTemp.Tables.Add(dvConfType.ToTable());
    ddlConfType2.DataSource = dsTemp;
    ddlConfType2.DataBind();
    //ddlBA.Attributes["onchange"] = "function GetDDLD(" + ddlConfType.ClientID + ", " + ddlConfType2.ClientID + ") {ModifyDDLItems(id1, id2);}";
    ddlBA.Attributes.Add("onchange", "ModifyDDLItems('" + ddlConfType.ClientID + "', '" + ddlConfType2.ClientID + "')");
}

当我运行它时,VS不断告诉我id1和id2都是null,似乎id没有正确传递给客户端。

我觉得你的代码写错了,我一眼就发现的第一个错误是

使用

无法在gridview中找到控件
gvRow.FindControl("ddlBA");

在GridView中可能有多行,所以你必须在每一行中找到你的控件,因为它们都有不同的clientid。首先尝试替换下面的代码

gvRow.Rows[RowIndex].FindControl("ControlID");

此外,它应该写在某种循环中,以便找到Grid的RowIndex值。

简要描述你的确切需求。所以,我可以帮助你在编写适当的代码。