详细信息视图插入时的默认值

发布于 2024-08-06 11:40:57 字数 282 浏览 3 评论 0原文

当用户转到指定页面时,我有一个附加到 SqlDataSource 的详细信息视图,并且已经设置为插入模式。我本质上是用它作为某些活动的注册页面。我不想让用户输入他们的“用户名”,而是希望该字段根据已登录的用户自动填充。

是否有人对如何让 User.Identity.Name 作为显示的默认值有任何建议在 Page_load 上,或者如何在 DetailsViewInsertEventArgs 上编写重写代码?当然,如果我完全偏离基地,那么其他建议会很棒。

我在后面使用 C# 代码。

谢谢, 麦克风

When users go to a specified page, I have a DetailsView attached to a SqlDataSource and already setup in Insert mode. I'm essentially using it as a registration page for certain events. Rather than having the users type in their "UserName", I want that field to automatically populate based on that user already being logged in.

Does anyone have any suggestions on how to either have the User.Identity.Name be the default value that appears on Page_load, or how to code an override on DetailsViewInsertEventArgs? Of course, if I'm totally off-base, then other suggestions would be great.

I'm using c# code behind.

Thanks,
Mike

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

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

发布评论

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

评论(4

歌枕肩 2024-08-13 11:40:57

你可以这样做:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DetailsView1.DefaultMode = DetailsViewMode.Insert;
            if (DetailsView1.FindControl("TextBox1") != null)
            {
                TextBox txt1 = (TextBox)DetailsView1.FindControl("TextBox1");
                txt1.Text = User.Identity.Name.ToString();
            }
        }
    }

You can do this:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DetailsView1.DefaultMode = DetailsViewMode.Insert;
            if (DetailsView1.FindControl("TextBox1") != null)
            {
                TextBox txt1 = (TextBox)DetailsView1.FindControl("TextBox1");
                txt1.Text = User.Identity.Name.ToString();
            }
        }
    }
浮华 2024-08-13 11:40:57

啊,谢谢里卡多。这是有道理的,但我仍然无法让它发挥作用。我想我忘了提及控件位于边界字段中,如果这有什么区别的话。

这是主页代码:

<div align="center">
    <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" 
        DataKeyNames="RegistrationID" DataSourceID="SqlDataSourceFBReg" 
        DefaultMode="Insert" Height="50px" 
        Width="55%" OnItemInserted="NFLElim" >
        <Fields>
            <asp:BoundField DataField="RegistrationID" HeaderText="RegistrationID" 
                InsertVisible="False" ReadOnly="True" SortExpression="RegistrationID" />
            <asp:BoundField DataField="UserName" HeaderText="One Season User Name" 
                SortExpression="UserName" />
            <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />...

这是我如何设置背后的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class NFLElim_Reg : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DetailsView1.DefaultMode = DetailsViewMode.Insert;
            if (DetailsView1.FindControl("UserName") != null)
            {
                TextBox txt1 = (TextBox)DetailsView1.FindControl("UserName");
                txt1.Text = User.Identity.Name.ToString();
            }
        }

    }
    protected void NFLElim(object sender, DetailsViewInsertedEventArgs e)
    {
        Response.Redirect("Account.aspx");
    }

}

希望我正确插入了代码

Ah, Thanks Ricardo. That makes sense, but I'm still having trouble getting it to work. I guess I forgot to mention that the control is in a boundfield, if that makes a difference.

Here's the mainpage code:

<div align="center">
    <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" 
        DataKeyNames="RegistrationID" DataSourceID="SqlDataSourceFBReg" 
        DefaultMode="Insert" Height="50px" 
        Width="55%" OnItemInserted="NFLElim" >
        <Fields>
            <asp:BoundField DataField="RegistrationID" HeaderText="RegistrationID" 
                InsertVisible="False" ReadOnly="True" SortExpression="RegistrationID" />
            <asp:BoundField DataField="UserName" HeaderText="One Season User Name" 
                SortExpression="UserName" />
            <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />...

And here's how I have the code behind setup:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class NFLElim_Reg : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DetailsView1.DefaultMode = DetailsViewMode.Insert;
            if (DetailsView1.FindControl("UserName") != null)
            {
                TextBox txt1 = (TextBox)DetailsView1.FindControl("UserName");
                txt1.Text = User.Identity.Name.ToString();
            }
        }

    }
    protected void NFLElim(object sender, DetailsViewInsertedEventArgs e)
    {
        Response.Redirect("Account.aspx");
    }

}

Hopefully I inserted the code correctly

差↓一点笑了 2024-08-13 11:40:57

TextBox tata = (TextBox)DetailsView1.Rows[6].Cells[1].Controls[0];
tata.Text=用户.身份.名称;

这将改变边界场。如果将绑定字段更改为模板字段,此代码也可以工作:

TextBox txt1 = (TextBox)DetailsView1.FindControl("UserName");
txt1.Text = User.Identity.Name.ToString();

TextBox tata = (TextBox)DetailsView1.Rows[6].Cells[1].Controls[0];
tata.Text=User.Identity.Name;

This will chane boundfield. If you change your boundfield to templatefield, this code can also work:

TextBox txt1 = (TextBox)DetailsView1.FindControl("UserName");
txt1.Text = User.Identity.Name.ToString();

薄荷→糖丶微凉 2024-08-13 11:40:57

不确定这是否是您想要的方式,但如果您想使用 Session,这是一个非常简单的修复方法。我其实只是自己研究这个。

如果您将用户名存储在 Session 中,则可以将其作为参数从那里提取到 SQLDataSource 中。

我所做的只是将 SessionParameter 添加到标记视图中的 InsertParameter 列表中。

对于您的情况,我建议转到 SqlDataSourceFBReg 控件的标记,并查找 InsertParameters 列表。

然后,添加一个 asp:SessionParameter,其中 Name="UserName" 和 SessionField="whatever the session name is"

Not sure if this is how you want to do it, but if you want to use Session, it is a really easy fix. I was actually just researching this myself.

If you store your username in Session, you can pull it right from there as a parameter into your SQLDataSource.

All I did was add a SessionParameter to my InsertParameter list in markup view.

In your case, I would recommend going to the markup for the SqlDataSourceFBReg control, and looking for the list of InsertParameters.

Then, add an asp:SessionParameter with Name="UserName" and SessionField="whatever the session name is"

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