DetailsView插入时的默认值

6
当用户进入指定页面时,我已经将DetailsView附加到SqlDataSource并设置为插入模式。 我基本上将其用作某些事件的注册页面。 我不想让用户输入他们的“用户名”,而是希望该字段根据已经登录的用户自动填充。
有没有人有关于如何使User.Identity.Name成为在Page_load上出现的默认值,或者如何编写DetailsViewInsertEventArgs的重写的建议? 当然,如果我完全错了,那么其他建议也很好。
我正在使用c#代码后台。
谢谢, 麦克

一些代码会帮助我们更好地理解你的问题。 - Phaedrus
4个回答

3
你可以这样做:
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();
            }
        }
    }

为了使这个工作起来,我不得不将代码放在一个OnDataBound事件处理程序中,而不是Page_Load中。 - seagulledge

0

啊,谢谢你Ricardo。这很有道理,但我仍然无法让它工作。我想我忘了提到控件是在绑定字段中,如果这有所不同。

这是主页面的代码:

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

}

希望我已经正确地插入了代码。

应该可以了,你正确地插入了代码。即使在绑定字段中,这也应该可以工作。 - Ricardo Sanchez
这个不起作用。FindControl只能在TemplateField内部的TextBox字段上起作用。 - seagulledge

0

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

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

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


0

不确定这是否是您想要的方式,但如果您想使用Session,那么这是一个非常简单的解决方法。我自己也在研究这个问题。

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

我所做的就是在标记视图中向我的InsertParameter列表中添加了一个SessionParameter。

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

然后,添加一个asp:SessionParameter,其中Name="UserName",SessionField="会话名称"


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接