DetailsView Insert上的默认值

时间:2009-10-01 00:17:42

标签: c# asp.net data-binding asp.net-membership

当用户转到指定页面时,我有一个附加到SqlDataSource的DetailsView,并且已经在插入模式下设置。我基本上将它用作某些事件的注册页面。我希望该字段根据已登录的用户自动填充,而不是让用户输入“UserName”。

有没有人建议如何让User.Identity.Name成为Page_load上显示的默认值,或者如何在DetailsViewInsertEventArgs上编码覆盖?当然,如果我完全偏离基础,那么其他建议就会很棒。

我正在使用c#代码。

谢谢, 麦克

4 个答案:

答案 0 :(得分: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();
            }
        }
    }

答案 1 :(得分:0)

啊,谢谢里卡多。这是有道理的,但我仍然无法让它工作。我想我忘了提到控件是在一个边界,如果这有所不同。

这是主页代码:

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

}

希望我正确插入代码

答案 2 :(得分:0)

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

这将是chane boundfield。如果将boundfield更改为templatefield,则此代码也可以起作用:

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

答案 3 :(得分:0)

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

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

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

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

然后,添加一个名为=&#34; UserName&#34;的asp:SessionParameter。和SessionField =&#34;无论会话名称是什么&#34;

相关问题