gpt4 book ai didi

c# - 从sql数据源获取数据到asp.net控件

转载 作者:行者123 更新时间:2023-11-30 15:12:21 26 4
gpt4 key购买 nike

我有一个带有非常简单的 select 语句的 sqldatasource,它应该总是返回一行。我想用数据源中的数据填充页面上的文本框。我怎么能为文本框做这个?如果数据库中没有数据,那么我希望文本框保持为空。我怎样才能做到这一点?

<asp:Panel ID = "Panel2" runat="server" DefaultButton = "save" >
<fieldset style="width: 524px"><legend>Rouse InterChange Details</legend>
<asp:FormView runat="server" ID="MyFormView" DataSourceID="SqlDataSource3" DefaultMode="Edit">
<ItemTemplate >
<table>
<tr>
<td align="right">Interchange ID:</td>
<td align="left">
<asp:TextBox ID="txtIntID" runat="server" size="1" MaxLength = "2" Text='<%# Bind("Interchange_Id") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator8" ValidationGroup = "rouse" Display ="None" ControlToValidate = "txtIntID" runat="server" ErrorMessage="You Must Provide an Interchange ID."> </asp:RequiredFieldValidator>
<ajaxToolkit:ValidatorCalloutExtender ID="ValidatorCalloutExtender11" TargetControlID="RequiredFieldValidator8" HighlightCssClass="validatorCalloutHighlight" runat="server">
</ajaxToolkit:ValidatorCalloutExtender>
</td>
</tr>
<tr>
<td align="right">Sender ID:</td>
<td align="left">
<asp:TextBox ID="txtsender" runat="server" MaxLength = "15" ></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator9" ValidationGroup = "rouse" Display ="None" ControlToValidate = "txtsender" runat="server" ErrorMessage="You Must Provide a Sender ID."> </asp:RequiredFieldValidator>
<ajaxToolkit:ValidatorCalloutExtender ID="vce12" TargetControlID="RequiredFieldValidator9" HighlightCssClass="validatorCalloutHighlight" runat="server">
</ajaxToolkit:ValidatorCalloutExtender>
</td>
</tr>
<tr>
<td align="right">Interchange Standard ID:</td>
<td align="left">
<asp:TextBox ID="ISI" runat="server" size="1" MaxLength = "1" ></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator10" ValidationGroup = "rouse" Display ="None" ControlToValidate = "ISI" runat="server" ErrorMessage="You Must Provide an Interchange Standard ID."> </asp:RequiredFieldValidator>
<ajaxToolkit:ValidatorCalloutExtender ID="ValidatorCalloutExtender12" TargetControlID="RequiredFieldValidator10" HighlightCssClass="validatorCalloutHighlight" runat="server">
</ajaxToolkit:ValidatorCalloutExtender>
</td>
</tr>
<tr>
<td align="right">Version:</td>
<td align="left">
<asp:TextBox ID="Verstxt" runat="server" size="5" MaxLength = "5" ></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator11" ValidationGroup = "rouse" Display ="None" ControlToValidate = "Verstxt" runat="server" ErrorMessage="You Must Provide a Version."> </asp:RequiredFieldValidator>
<ajaxToolkit:ValidatorCalloutExtender ID="ValidatorCalloutExtender13" TargetControlID="RequiredFieldValidator11" HighlightCssClass="validatorCalloutHighlight" runat="server">
</ajaxToolkit:ValidatorCalloutExtender>
</td>
</tr>
<tr>
<td align="right">Functional ID:</td>
<td align="left">
<asp:TextBox ID="FuncID" runat="server" size="1" MaxLength = "2" ></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator12" ValidationGroup = "rouse" Display ="None" ControlToValidate = "FuncID" runat="server" ErrorMessage="You Must Provide a Functional ID."> </asp:RequiredFieldValidator>
<ajaxToolkit:ValidatorCalloutExtender ID="ValidatorCalloutExtender14" TargetControlID="RequiredFieldValidator12" HighlightCssClass="validatorCalloutHighlight" runat="server">
</ajaxToolkit:ValidatorCalloutExtender>
</td>
</tr>
<tr style="display:none">
<td align="right">Group Control #</td>
<td align="left">
<asp:TextBox ID="txtGroupcontrol" runat="server" size="6" MaxLength = "9" ></asp:TextBox>

</td>
</tr>
</table>
</ItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="connectionstring"
SelectCommand="select * from table1 "></asp:SqlDataSource>
</fieldset></asp:Panel>

最佳答案

您应该考虑使用 FormView 控件来执行此操作。与尝试将所有代码塞入页面的代码隐藏相比,使用绑定(bind)控件更容易、更清晰。我还觉得它使页面更易于维护,因为您也不需要管理所有 C#/VB 代码。

<asp:FormView runat="server" ID="MyFormView" DataSourceID="MySqlDataSource" DefaultMode="Edit">
<EditItemTemplate>
<table>
<tr>
<td align="right">Interchange ID:</td>
<td align="left">
<asp:TextBox ID="txtIntID" runat="server" Text='<%# Bind("InterchangeID") %>' size="1" MaxLength = "2"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator8" ValidationGroup = "rouse" Display ="None" ControlToValidate = "txtIntID" runat="server" ErrorMessage="You Must Provide an Interchange ID."></asp:RequiredFieldValidator>
<ajaxToolkit:ValidatorCalloutExtender ID="ValidatorCalloutExtender11" TargetControlID="RequiredFieldValidator8" HighlightCssClass="validatorCalloutHighlight" runat="server"> </ajaxToolkit:ValidatorCalloutExtender>
</td>
</tr>

<!-- rest of your table here -->
</table>
</EditItemTemplate>
</asp:FormView>
<asp:SqlDataSource runat="server" ID="MySqlDataSource"
SelectCommand="SELECT * FROM MyTable" ConnectionString="<%$ ConnectionStrings:MyConnectionString %>" >
</asp:SqlDataSource>

有关更多信息,请参阅以下链接:

关于c# - 从sql数据源获取数据到asp.net控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1357709/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com