繁体   English   中英

当控件从ASPX页面传递时,用户控件内的复选框将变为null

[英]Checkboxlist inside a usercontrol going as null when control passed from an aspx page

再次陷入了一个场景。 摘要如下:我有一个用户控件,它基本上是一个文本框,图像按钮,复选框列表的组合,看起来像带有复选框的单选下拉列表。 用户控件内的图像之一会打开aspx页面作为弹出窗口。那里的功能很少,即可以将值保存到数据库和填充物中。 在弹出页面的“确定”按钮上单击,我应该能够将值保存到数据库,并用保存到数据库的值填充usercontrol(充当下拉菜单)。 在这里出现问题,当尝试将checkboxlist(存在于usercontrol中)绑定到数据库中的值时,我得到一个错误,即checkboxlist对象为null并且尚未创建。 我感觉单击“确定”按钮时,用户控件必须刷新,因此复选框列表将处于活动状态。

PFB的相关代码:Usercontrol.ascx:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="SingleSelectCustomDropDown.ascx.cs" Inherits="MS.IT.Informa.UI.UserControls.SingleSelectCustomDropDown" %>

<asp:Panel ID="panel" runat="server">
<div id="FirstDiv">
<table>
<tr>
<td align="right">
<asp:TextBox ID="txtSelect" runat="server" ReadOnly="true"></asp:TextBox>
</td>
<td>
<asp:Image ID="imgShow" ImageUrl="../Images/DDGlyph.png" onmouseover="this.src='../Images/DDGlyphHOVER.png'" onmouseout="this.src='../Images/DDGlyph.png'" runat="server" />
</td>
</tr>
<tr>
<td colspan="2">
<div id="SecondDiv" style="display:none;">
<asp:CheckBoxList ID="chkBoxList" runat="server">
<asp:ListItem Value="0" Text="Standard" Selected="True"></asp:ListItem>
</asp:CheckBoxList>
</div>
<div id="ThirdDiv" style="display:none;">
<asp:ImageButton ID="btnNew" runat="server" Height="20px" ImageUrl="~/Images/new.png" Width="20px" OnClientClick="ShowPopup();" />
<asp:ImageButton ID="btnEdit" runat="server" Height="20px" ImageUrl="~/Images/edit.png" Width="20px" />
<asp:ImageButton ID="btnDefault" runat="server" Height="20px" ImageUrl="~/Images/default.png" Width="20px" />
<asp:ImageButton ID="btnDelete" runat="server" Height="20px" ImageUrl="~/Images/delete.png" Width="20px" />
</div>
</td>
</tr>
</table>
</div>
</asp:Panel>
<script type="text/javascript">
//Displays the divs containing checkboxlist and images
function ShowList() {
    document.getElementById("SecondDiv").style.display = "block";
    document.getElementById("ThirdDiv").style.display = "block";
}
//Hides the divs containing checkboxlist and images
function HideList() {
    document.getElementById("SecondDiv").style.display = "none";
    document.getElementById("ThirdDiv").style.display = "none";
}
//Displays the selected item from the checkboxlist into the textbox placed in the Custom Control
function DisplaySelectedItem(sender, txtBoxID) {

    var x = document.getElementById(sender.id);
    var chkBoxPrefix = sender.id + "_";
    var selectedText;
    for (i = 0; i < x.rows.length; i++) {
    if(document.getElementById(chkBoxPrefix+i).checked)
    {
        selectedText = document.getElementById(chkBoxPrefix+i).parentNode.innerText;
    }
    }
    document.getElementById(txtBoxID.id).value = selectedText;
}
//Ensures that only one item is selected from the checkboxlist
function SelectOnlyOneCheckBox(e) {

    if (!e) e = window.event;
    var sender = e.target || e.srcElement;
    if (sender.nodeName != 'INPUT') {
        return;
    }
    var checker = sender;
    var chkBox = document.getElementById('<%= chkBoxList.ClientID %>');
    var chks = chkBox.getElementsByTagName('INPUT');
    for (i = 0; i < chks.length; i++) {
        if (chks[i] != checker)
            chks[i].checked = false;
    }
}
function ShowPopup() {
    window.open("ViewColumnOptions.aspx", "ViewColumnOptions", "height=300,width=600,left=300,top=150");
}
</script>

用户控件的代码如下:

public partial class SingleSelectCustomDropDown : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
    {
            if (!IsPostBack)
        {

           chkBoxList.Attributes.Add("onclick", "SelectOnlyOneCheckBox(event);DisplaySelectedItem(this," + txtSelect.ClientID + ");HideList();");
            txtSelect.Attributes.Add("onclick", "ShowList();");
            imgShow.Attributes.Add("onclick", "ShowList();");
        }
    }
    public void PopulateOtherViews()
    {
        SaveReportViewFilter<ReportFilterBase> newObj = new SaveReportViewFilter<ReportFilterBase>();
        ViewColumnOptions vwobj = new ViewColumnOptions();
        newObj.UserName = vwobj.Page.User.Identity.Name;
        SaveReportView<ReportFilterBase> obj2 = new SaveReportView<ReportFilterBase>();
        DataTable dt = obj2.GetSaveReportViewFromDataBase(newObj);
        chkBoxList.DataSource = dt;//chkBoxList becomes null here..we have ample data in the datatable though
        chkBoxList.DataTextField = dt.Columns[0].ToString();
        chkBoxList.DataValueField = dt.Columns[0].ToString();
        chkBoxList.DataBind();
    }
}

在aspx页面的按钮单击上调用PopulateOtherViews函数。 下面是代码:

    protected void btnOK_Click(object sender, EventArgs e)
    {
        if (chkSaveView.Checked)
        {
            if (!string.IsNullOrEmpty(txtViewName.Text))
            {
                //some code here to save the view name from txtViewName to the DB
                SingleSelectCustomDropDown obj22 = new SingleSelectCustomDropDown();
                obj22.PopulateOtherViews();
                Page.ClientScript.RegisterStartupScript(this.GetType(),"close","CloseWindow();",true);

         }
            else
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alertEnterViewName", "alertMessage('Please enter the view name');", true);
            }

        }

    }

任何帮助,建议,指示将不胜感激。 问候阿努拉格

在btnOK_Click中,您正在创建用户控件的新实例,而不是将其附加到页面上。 我的建议是:

1.注册用户控件并将其添加到页面。

<%@ Register Src="~/UserControl/SingleSelectCustomDropDown.ascx" TagPrefix="uc1" TagName="SingleSelectCustomDropDown" %>

还有...

<uc1:SingleSelectCustomDropDown runat="server" id="obj22" /> 

2.现在在后面的代码中进行修改:

protected void btnOK_Click(object sender, EventArgs e)
{    
    if (chkSaveView.Checked)
    {
        if (!string.IsNullOrEmpty(txtViewName.Text))
        {
            //some code here to save the view name from txtViewName to the DB
            //Do not create the control again
            //SingleSelectCustomDropDown obj22 = new SingleSelectCustomDropDown();
            obj22.PopulateOtherViews();
            Page.ClientScript.RegisterStartupScript(this.GetType(), "close", "CloseWindow();", true);

        }
        else
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "alertEnterViewName", "alertMessage('Please enter the view name');", true);
        }

    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM