上控制使用的FindControl

上控制使用的FindControl

本文介绍了上控制使用的FindControl()内面板返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始用一种形式,两个面板,面板1和是Panel2的页面。是Panel2包含动态生成基于用户对Panel1的输入服务器的主机名列表上的控件。 $ P在面板1 $ pssing提交设置Panel1的可见性设置为false,是Panel2的知名度为true。 pressingSubmit2关于使用是Panel2所有字段的按钮后由用户填写的,我想从这些控件进行处理的信息。不过,我使用的FindControl(symptoms_+ i.ToString())(类似的规定,根据每个控制名称),但它返回null。

I start with a page with a form and two panels, Panel1 and Panel2. Panel2 has dynamically generated controls based on a list of server hostnames that the user enters on Panel1. Pressing "submit" on Panel1 sets Panel1's visibility to false and Panel2's visibility to true. After pressing "Submit2" button on Panel2 with all of the fields filled out by the user, I want to get the information from these controls for processing. However, I use FindControl("symptoms_" + i.ToString()) (something along those lines, depending on each control name), but it is returning null.

下面是我入手的形式,生成控件之前:

Here is the form I start with, before controls are generated:

<form id="btil_form" runat="server">
    <div>
    <asp:Panel ID="Panel2" runat="server" Visible="False">
        <asp:Button ID="Submit2" runat="server" Text="Submit" OnClick="Submit2_Click" />
        <br />
        <asp:Literal ID="result" runat="server"></asp:Literal>
        <br />
    </asp:Panel>
</form>

然后生成控件后,页面的源代码显示每个控制正确的ID值:

Then after the controls are generated, the page source shows the correct ID values for each control:

<div id="Panel2">

    <input type="submit" name="Submit2" value="Submit" id="Submit2" />
    <br />

    Hostname: g1x5554<br />Issue Reported:
    <select name="issue_1" id="issue_1">
        <option value="blank"></option>
        <!-- snip -->
        <option value="VC Profile Issue">VC Profile Issue</option>
    </select>

<br />
Symptoms: <textarea name="symptoms_1" rows="2" cols="20" id="symptoms_1"></textarea>
<br />
Problem Notes: <textarea name="notes_1" rows="2" cols="20" id="notes_1"></textarea>

***** snip *****

</div>

下面是用来添加这些控件code的样本。 symptomsList是一个文本框,包含我加入文本框。

Here is a sample of the code used to add these controls. symptomsList is a TextBox, containing the textboxes I am adding.

Panel2.Controls.Add(new Literal() { Text = "Symptoms: " });
Panel2.Controls.Add(symptomsList[litList.IndexOf(singleItem)]);

然后我去遍历与每个主机相关的控制。每个控件具有相同的ID,因此对于控制症状和注意事项,每个字段的ID将是symptoms_1和NOTES_1为第一个主机,symptoms_2和notes_2的第2主机, 等等。在循环中,我试图让像这样的控制值:

Then I go to loop through the controls associated with each host. Each of these controls has the same ID, so for controls "symptoms" and "notes", the IDs for each field will be "symptoms_1" and "notes_1" for the first host, "symptoms_2" and "notes_2" for the 2nd host, and so on. Within the loop, I try to get the control values like so:

TextBox thisTB = new TextBox();
thisTB = (TextBox)Panel2.FindControl("symptoms_" + i.ToString());
thisBTIL.symptoms = thisTB.Text;

不过,显然很明显的FindControl返回空值,并试图为一个TextBox抛出一个NullReferenceException当它达到投空 thisBTIL.symptoms = thisTB.Text;

任何帮助将大大AP preciated!非常感谢。

Any help will be greatly appreciated! Many thanks.

推荐答案

我会建议你重写逻辑铸造这样的:

I would recommend rewriting your casting logic to this:

TextBox thisTB = Panel2.FindControl("symptoms_" + i.ToString()) as TextBox;

// Check to make sure the text box exists before we try to use it
if(thisTB != null)
{
    thisBTIL.symptoms = thisTB.Text;
}

如果转换失败的运营商不抛出一个异常,而是返回,因此需要在检查如果

The as operator does not throw an exception if the cast fails, but instead returns null, thus the need to check for null in the if.

这篇关于上控制使用的FindControl()内面板返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 17:51