当用户转到指定的页面时,我有一个连接到SqlDataSource的DetailsView,并且已经设置为插入模式。我本质上是将它用作某些事件的注册页面。我不想让用户键入他们的"UserName",而是希望根据已经登录的用户自动填充该字段。
有没有人对如何让User.Identity.Name成为Page_load上显示的默认值,或者如何在DetailsViewInsertEventArgs上编写重写代码有什么建议?当然,如果我完全错了,那么其他的建议也会很棒。
我在后台使用c#代码。
谢谢,迈克
发布于 2009-10-03 01:30: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();
}
}
}
发布于 2009-10-03 14:47:29
啊,谢谢里卡多。这是有道理的,但我仍然有问题让它工作。我想我忘了提到控件在边界域中,如果这有区别的话。
下面是主页面代码:
<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" />...
下面是我如何在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");
}
}
希望我正确地插入了代码
发布于 2010-08-08 17:21:51
TextBox tata = (TextBox)DetailsView1.Rows6.Cells1.Controls;tata.Text=User.Identity.Name;
这将改变边界域。如果您将边界字段更改为templatefield,则此代码也可以工作:
txt1 txt1= (TextBox)DetailsView1.FindControl("UserName");txt1.Text =();
https://stackoverflow.com/questions/1501310
复制相似问题