访问详细信息视图中的文本框

发布于 2024-12-04 21:01:01 字数 694 浏览 0 评论 0原文

有谁知道如何在 DetailsView 字段(c#)内设置文本框的文本?

我尝试了一些变体,但所有变体都返回脱离上下文和对象引用未设置错误。

这是我的代码...

ASPX:

<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px">
    <Fields>
        <asp:TemplateField HeaderText="Header Text">
            <InsertItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" Text="test"></asp:TextBox>
            </InsertItemTemplate>
        </asp:TemplateField>
    </Fields>
</asp:DetailsView>

CS:

TextBox txt = (TextBox)DetailsView1.FindControl("TextBox1");
txt.Text = "Text ";

干杯

Does anyone have any idea how I can set the text of a textbox inside a DetailsView field (c#)?

I've tried a few variations but all return out of context and object reference not set errors.

Heres my code...

ASPX:

<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px">
    <Fields>
        <asp:TemplateField HeaderText="Header Text">
            <InsertItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" Text="test"></asp:TextBox>
            </InsertItemTemplate>
        </asp:TemplateField>
    </Fields>
</asp:DetailsView>

CS:

TextBox txt = (TextBox)DetailsView1.FindControl("TextBox1");
txt.Text = "Text ";

Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

轻许诺言 2024-12-11 21:01:01

该错误可能意味着您的 DetailsView 中还没有任何内容。我敢打赌,如果您将其放入代码中:

TextBox txt = (TextBox)DetailsView1.FindControl("TextBox1");
if (txt != null)
{
    txt.Text = "Text ";
}

您会看到 txt 实际上是 null - 这是因为 FindControl 方法在 DetailsView 中未找到任何名为“TextBox1”的内容。

您需要将此代码移动到您知道 DetailsView 已填充的位置(如果它绑定到 DataSource,您可以在 DataBound< DetailsView 的 /code> 事件)。

另外,我注意到您的 TextBox 位于 InsertItemTemplate 中。在默认情况下将 DetailsView 置于编辑模式之前,您不会找到 TextBox1

<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px"  
        DefaultMode="Insert">

DetailsView1.ChangeMode(DetailsViewMode.Insert);
TextBox txt = (TextBox)DetailsView1.FindControl("TextBox1"); 
txt.Text = "Text "; 

希望这会有所帮助。

That error likely means that there is nothing in your DetailsView yet. I bet if you put this in your code:

TextBox txt = (TextBox)DetailsView1.FindControl("TextBox1");
if (txt != null)
{
    txt.Text = "Text ";
}

You'll see that txt is, in fact, null - this is because the FindControl method didn't find anything named "TextBox1" in the DetailsView.

You need to move this code to a point where you know the DetailsView has been populated (if it's bound to a DataSource, you could do this in the DataBound event of your DetailsView).

Also, I noticed that your TextBox is in an InsertItemTemplate. You won't find that TextBox1 until you put the DetailsView in edit mode, either by default:

<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px"  
        DefaultMode="Insert">

Or in code behind:

DetailsView1.ChangeMode(DetailsViewMode.Insert);
TextBox txt = (TextBox)DetailsView1.FindControl("TextBox1"); 
txt.Text = "Text "; 

Hopefully that helps.

新一帅帅 2024-12-11 21:01:01

@jadarnel27 感谢您提供有关将代码放入 DataBound 事件中的信息。我遇到了类似的问题,文本框值只能通过按钮单击事件传递到 DetailsView 字段 (DefaultMode=Insert)。我不想/不需要按钮。

我的场景:代码标识是否需要详细视图(用于插入)。如果根据从下拉控件中选择的人员不存在记录,则其 PersonID 会预加载到详细信息视图的人员字段中。

使用 FindControl 不起作用,但使用以下代码在放入 DataBound 控件时起作用。

// Get person selected from dropdown control and pass to DetailsView first row/cell
txtPerson.Text = Convert.ToString(ddlPersonSelect.SelectedValue);

TextBox tb = (TextBox)DetailsView1.Rows[0].Cells[1].Controls[0];
if (tb != null)
{
tb.Text = txtPerson.Text;
}

我希望这可以帮助像我这样的另一个新手。

@jadarnel27 thank you for your information about putting the code in DataBound event. I had similar issue where a textbox value would only pass to DetailsView field (DefaultMode=Insert) with a button click event. I didn't want/need a button.

My scenario: code identifies if detailsview is needed (for insert). If record does not exist based on person selected from dropdown control, their PersonID is preloaded into the Person field of detailsview.

Using FindControl didn't work but using the following code did WHEN put in DataBound control.

// Get person selected from dropdown control and pass to DetailsView first row/cell
txtPerson.Text = Convert.ToString(ddlPersonSelect.SelectedValue);

TextBox tb = (TextBox)DetailsView1.Rows[0].Cells[1].Controls[0];
if (tb != null)
{
tb.Text = txtPerson.Text;
}

I hope this helps another newbie like me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文