C#DetailsView获取值

发布于 2024-12-11 21:50:34 字数 1307 浏览 0 评论 0原文

我试图从我的数据库中获取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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

且行且努力 2024-12-18 21:50:34

如果没有更多上下文,很难准确说出问题所在,但我认为 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 the Controls collection on Cells[1] as the DetailsView renders the bound fields as TextBoxes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文