2

再次进入场景。总结如下:我有一个用户控件,它基本上是一个文本框、图像按钮、复选框列表的混合体,看起来像一个带有复选框的单选下拉列表。工作得很好。用户控件中的其中一个图像打开了一个 aspx 页面作为弹出窗口。那里几乎没有功能,即将值保存到数据库和其他东西。在弹出页面的 OK 按钮上单击,我应该能够将值保存到数据库,并使用我保存到数据库的值填充用户控件(充当下拉列表)。这里出现了问题,当尝试将复选框列表(存在于用户控件中)绑定到数据库中的值时,我收到复选框列表对象为空且尚未创建的错误。我觉得在确定按钮上单击,

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);
            }

        }

    }

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

4

1 回答 1

0

在 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);
        }

    }
}
于 2013-07-25T00:48:04.953 回答