![](/img/trans.png)
[英]Is there a switch to get the default values of a SQL Server's table's fields as default values for TextBoxes of a FormView / DetailsView?
[英]Default Values on DetailsView Insert
当用户转到指定页面时,我将一个DetailsView附加到SqlDataSource并已在Insert模式下设置。 我基本上将它用作某些事件的注册页面。 我希望该字段根据已登录的用户自动填充,而不是让用户输入“UserName”。
有没有人有任何关于如何让User.Identity.Name成为Page_load上显示的默认值,或者如何在DetailsViewInsertEventArgs上编码覆盖的建议? 当然,如果我完全偏离基础,那么其他建议就会很棒。
我正在使用c#代码。
谢谢,迈克
你可以这样做:
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();
}
}
}
啊,谢谢里卡多。 这是有道理的,但我仍然无法让它工作。 我想我忘了提到控件是在一个边界,如果这有所不同。
这是主页代码:
<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");
}
}
希望我正确插入代码
不确定这是不是你想要这样做,但如果你想使用Session,这是一个非常简单的解决方案。 我其实只是在研究这个问题。
如果您将用户名存储在Session中,则可以将其作为参数直接从中拉入SQLDataSource。
我所做的就是在标记视图中将一个SessionParameter添加到我的InsertParameter列表中。
在您的情况下,我建议转到SqlDataSourceFBReg控件的标记,并查找InsertParameters列表。
然后,添加一个名为=“UserName”的asp:SessionParameter和SessionField =“无论会话名称是什么”
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.