C#DetailsView获取值
我试图从我的数据库中获取userID
,将其放入DetailsView
并获取值并将其设置为标签。
我收到一个错误,字符串为空。
public void SendButton_Click(object sender, EventArgs e)
{
labelID.Text = DetailsView1.Rows[0].Cells[1].Text;
strVariable = labelID.Text;
System.Diagnostics.Debug.Write("variabelen: " + strVariable);
System.Guid g = new Guid(strVariable);
SqlDataSource1.InsertParameters[3].DefaultValue = g.ToString();
SqlDataSource1.Insert();
}
<asp:Label ID="labelID" Name="LabelID" runat="server"></asp:Label>
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
DataSourceID="SqlDataSource2" Height="50px" Width="125px"
DefaultMode="Edit">
<Fields>
<asp:BoundField DataField="UserId" HeaderText="UserId"
SortExpression="UserId" />
</Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT UserId FROM aspnet_Users WHERE (UserName = 'bo')">
</asp:SqlDataSource>
请注意,labelID.Text
位于单击按钮事件中,Detailview
(网格)加载是在用户登录时加载的。用户 ID 已加载到 中>detailsview1
当用户登录时。
我该如何解决这个问题?
Im trying to get the userID
out of my database put it into a DetailsView
and get the value and set it to a label.
Im getting an error that the the string is empty.
public void SendButton_Click(object sender, EventArgs e)
{
labelID.Text = DetailsView1.Rows[0].Cells[1].Text;
strVariable = labelID.Text;
System.Diagnostics.Debug.Write("variabelen: " + strVariable);
System.Guid g = new Guid(strVariable);
SqlDataSource1.InsertParameters[3].DefaultValue = g.ToString();
SqlDataSource1.Insert();
}
<asp:Label ID="labelID" Name="LabelID" runat="server"></asp:Label>
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
DataSourceID="SqlDataSource2" Height="50px" Width="125px"
DefaultMode="Edit">
<Fields>
<asp:BoundField DataField="UserId" HeaderText="UserId"
SortExpression="UserId" />
</Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT UserId FROM aspnet_Users WHERE (UserName = 'bo')">
</asp:SqlDataSource>
Note that the labelID.Text
is in a click button event and that the Detailview
(grid) loading is when the user is logged in. The user id get's loaded in detailsview1
when the user is logged in.
How can i fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果没有更多上下文,很难准确说出问题所在,但我认为
DetailsView1.Rows[0].Cells[1].Text
不是在 DetailsView 中查找的正确位置用户身份。您可以尝试在 Visual Studio 中的该行上设置断点,并逐步浏览直接窗口中的行和单元格以找到正确的位置。如果您可以提供用作DetailsView 数据源的代码以及DetailsView 的定义,那将会很有帮助。
基于新示例代码进行编辑:
当我运行示例代码时,
( DetailsView1.Rows[0].Cells[1].Controls[0] as TextBox ).Text
为我提供用户 ID 值。您必须进入Cells[1]
上的Controls
集合,因为 DetailsView 将绑定字段呈现为文本框。It's hard to say exactly what the problem is without more context, but I would assume that
DetailsView1.Rows[0].Cells[1].Text
is not the proper location within the DetailsView to look for the user id. You could try setting a breakpoint on that line in Visual Studio and stepping through the rows and cells in the immediate window to find the proper location.If you could provide the code you use as the data source for the DetailsView as well as the definition of the DetailsView that would be helpful.
EDIT BASED ON NEW SAMPLE CODE:
When I run your sample code,
( DetailsView1.Rows[0].Cells[1].Controls[0] as TextBox ).Text
gives me the user id value. You have to go into theControls
collection onCells[1]
as the DetailsView renders the bound fields as TextBoxes.