我已经在ajax ModalpopupExtender中输入了一个下拉列表并从表中填充,然后我需要选择下拉列表选择项的ID?
myfile.aspx.cs string MyQuery = string.Format(“select table from table where colName ='{0}'”,dropdown.SelectedItem.Text); SqlConnection con = DatabaseConnection.CreateConnection(); SqlCommand cmd = new SqlCommand(MyQuery,con); 字符串MyId; MyId = cmd.ExecuteScalar()as string;
//我也试过这个 int id = dropdownList.DataValueField;
请回复我,如何在执行select查询后以及填充数据表或数据集时从表中获取id?谢谢aloooot。
答案 0 :(得分:0)
这不是我的原始代码段。当我在messagebox中显示MyId变量时它是空的,并且在插入查询之后它说插入语句与外键约束冲突。这意味着代码不会获取我需要放在子表中的外键中的id。
这是我的代码隐藏文件 string MyQuery = string.Format("从District中选择DistrictId,其中DistrictName =' {0}'",ddlAddDistrict.SelectedItem.Text); SqlConnection con = DatabaseConnection.CreateConnection(); SqlCommand cmd = new SqlCommand(MyQuery,con); 字符串MyId; MyId = cmd.ExecuteScalar()as string; //Response.Write("alert('" + ddlAddDistrict.SelectedItem.Text +"')"); Response.Write(" alert('" + MyId +"')"); string insert ="插入PoliceStation值('" + MyId +"''" + policeStation +"' ;)&#34 ;; InsertIntoDB.InsertQuery(插入); if(InsertIntoDB.count> 0) { Response.Write(" alert('记录添加成功')"); InsertIntoDB.count = 0; } 其他{ Response.Write(" alert('错误:记录未添加')");
}
this is my .aspx file
<% - ============================派出所的模态弹出窗口========= ============= - %>
<AddNewPopup:ModalPopupExtender ID="mp1" runat="server" PopupControlID="PanlAddNewPS" TargetControlID="lbtnAddNewPS"
CancelControlID="btncanceladdNewPs" BackgroundCssClass="Background" OnLoad="mp1_Load">
</AddNewPopup:ModalPopupExtender>
<asp:Panel ID="PanlAddNewPS" runat="server" CssClass="PopupPoliceStation panel-info" align="center" Style="display: none">
<%--<a style="width: 600px; height: 200px;" id="irm1" src="PSDialog.aspx" runat="server"></a>--%>
<div class="panel-heading">
<div class="panel-title">Add New Police Station</div>
</div>
<br />
<div class="row">
<div class="col-sm-12">
<div class="form-group">
<asp:Label runat="server" Text="Add District" Font-Bold="true" ID="Label2" CssClass="col-sm-4 control-label" ></asp:Label>
<div class="col-sm-8">
<asp:DropDownList runat="server" ID="ddlAddDistrict" CssClass="form-control" MaxLength="20" OnSelectedIndexChanged="ddlAddDistrict_SelectedIndexChanged"></asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server" ControlToValidate="ddlAddDistrict" ErrorMessage="Select district" Font-Size="Small" ForeColor="Red" ValidationGroup="ValidateAddNewPoliceSation" InitialValue="Select New District" ></asp:RequiredFieldValidator>
</div> <br /><br />
<asp:Label runat="server" Text="Police Station" Font-Bold="true" ID="lblPSName" CssClass="col-sm-4 control-label" ></asp:Label>
<div class="col-sm-8">
<asp:TextBox runat="server" ID="txtAddPSName" CssClass="form-control" MaxLength="15"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server" ControlToValidate="txtAddPSName" ErrorMessage="Required" Font-Size="Small" ForeColor="Red" ValidationGroup="ValidateAddNewPoliceSation" ></asp:RequiredFieldValidator>
</div> <br /><br />
</div>
</div>
</div>
<asp:Button runat="server" CssClass="btn btn-success" ID="btnAddNewPS" Text="Add" ValidationGroup="ValidateAddNewPoliceSation" OnClick="btnAddNewPS_Click" />
<asp:Button CssClass="btn btn-info" ID="btncanceladdNewPs" runat="server" Text="Cancel" CausesValidation="false" />
</asp:Panel>