我有一个问题,因为我创建了三个级联下拉列表,显示了国家和州和城市,但我想保存下拉列表显示,如果您点击以下链接,您可以获得更多信息,因为我记录了什么正在发生在我身上。感谢您的支持。
https://www.youtube.com/watch?v=PAx3cvHqKf0&feature=youtu.be
ASP.NET标记
<div class="banner728">
<asp:Image ID="Image1" runat="server"
ImageUrl="~/images/general/bann72890.png" />
</div>
<div class="space">
</div>
<div class="style6">
<strong style="font-size: medium">Registre New User</strong></div>
<br />
<br />
<br />
<br />
<table style="width: 69%; height: 338px;" align="center">
<tr>
<td class="style5"> Användarnamn</td>
<td style="text-align: left" class="style8">
<asp:TextBox ID="UsrNme" runat="server" Width="258px" Height="20px"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="Userreqlbl" runat="server"
ControlToValidate="UsrNme" ErrorMessage="This field is required" ForeColor="Red"></asp:RequiredFieldValidator><br />
<br />
</td>
</tr>
<tr>
<td class="style5">
Förnamn
</td>
<td style="text-align: left" class="style8">
<asp:TextBox ID="fnbox" runat="server" Height="20px" Width="258px"
></asp:TextBox>
</td>
<td style="text-align: left">
</td>
</tr>
<tr>
<td class="style5">
Efternamn
</td>
<td style="text-align: left" class="style8">
<asp:TextBox ID="lnamebox" runat="server" Height="20px" Width="258px"></asp:TextBox>
</td>
<td style="text-align: left">
</td>
</tr>
<tr>
<td class="style5">
Lösenord
</td>
<td style="text-align: left" class="style8">
<asp:TextBox ID="passtxtbx1" runat="server" TextMode="Password" Width="258px" Height="20px"></asp:TextBox>
</td>
<td style="text-align: left">
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="passtxtbx1"
ErrorMessage="This part is required" ForeColor="Red"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="style5">
Återskrivnings Lösenord
</td>
<td style="text-align: left" class="style8">
<asp:TextBox ID="passtxtbx2" runat="server" TextMode="Password" Width="258px" Height="20px"></asp:TextBox>
</td>
<td style="text-align: left">
<asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="passtxtbx1"
ControlToValidate="passtxtbx2" ErrorMessage="The password is not match" ForeColor="Red"></asp:CompareValidator>
</td>
</tr>
<tr>
<td class="style5">
E-post
</td>
<td style="text-align: left" class="style8">
<asp:TextBox ID="emailbox" runat="server" Height="20px" Width="258px"></asp:TextBox>
</td>
<td style="text-align: left">
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="emailbox"
ErrorMessage="Invalid Format" ForeColor="Red" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td class="style5">
Land
</td>
<td style="text-align: left" class="style8">
<asp:DropDownList ID="countrdrdolst" runat="server" AutoPostBack="True"
onselectedindexchanged="countrdrdolst_SelectedIndexChanged">
</asp:DropDownList>
</td>
<td style="text-align: left">
</td>
</tr>
<tr>
<td class="style5">
Län
</td>
<td style="text-align: left" class="style8">
<asp:DropDownList ID="statedrdolst" runat="server" AutoPostBack="True"
onselectedindexchanged="statedrdolst_SelectedIndexChanged">
</asp:DropDownList>
</td>
<td style="text-align: left">
</td>
</tr>
<tr>
<td class="style5">
Stad
</td>
<td style="text-align: left" class="style11">
<asp:DropDownList ID="citiesdrdolst" runat="server" AutoPostBack="True"
onselectedindexchanged="citiesdrdolst_SelectedIndexChanged">
</asp:DropDownList>
</td>
<td style="text-align: left" class="style12">
</td>
</tr>
<tr>
<td class="style5">
Postnummer
</td>
<td style="text-align: left" class="style8">
<asp:TextBox ID="postbox" runat="server" Width="258px"
Height="20px"></asp:TextBox>
</td>
<td style="text-align: left">
</td>
</tr>
<tr>
<td class="style5">
Personlig bild
</td>
<td style="text-align: left" class="style8">
<asp:FileUpload ID="FileUpload1" runat="server" />
</td>
<td style="text-align: left">
</td>
</tr>
<tr>
<td>
</tr>
<tr>
<td class="style5">
</td>
<td class="style8">
<asp:Button ID="btnSave" runat="server" onclick="btnSave_Click" Text="Create" CausesValidation="True"/>
</td>
<td>
</td>
</tr>
</table>
<br />
<br />
<br />
</ContentTemplate>
</asp:UpdatePanel>
C#代码
public partial class Registration : System.Web.UI.Page
{
string sc = ConfigurationManager.ConnectionStrings["BeravaConnectionString"].ConnectionString.ToString();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataTable countrycascd = new DataTable();
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["BeravaConnectionString"].ConnectionString))
{
SqlDataAdapter adaptar = new SqlDataAdapter("select [countryID],[country] FROM [countr]", con);
adaptar.Fill(countrycascd);
countrdrdolst.DataSource = countrycascd;
countrdrdolst.DataTextField = "country";
countrdrdolst.DataValueField = "countryID";
countrdrdolst.DataBind();
}
countrdrdolst.Items.Insert(0, new ListItem("Select country", "0"));
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection(sc);
SqlCommand cmd = new SqlCommand();
string sqlstatment = "INSERT INTO UserInfo (UID, FN, LN, Password, RePass, Email,CountID, State,City, Post, Img) VALUES (@UID,@FN,@LN,@Password,@RePass,@Email,@CountID,@State,@City,@Post,@Img)";
cmd.Connection = cn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = sqlstatment;
//Insert the parameters first
cmd.Parameters.AddWithValue("@UID", UsrNme.Text);
cmd.Parameters.AddWithValue("@FN", fnbox.Text);
cmd.Parameters.AddWithValue("@LN", lnamebox.Text);
cmd.Parameters.AddWithValue("@Password", passtxtbx1.Text);
cmd.Parameters.AddWithValue("@RePass", passtxtbx2.Text);
cmd.Parameters.AddWithValue("@Email", emailbox.Text);
cmd.Parameters.AddWithValue("@CountID", countrdrdolst.Text);
cmd.Parameters.AddWithValue("@State", statedrdolst.Text);
cmd.Parameters.AddWithValue("@City", citiesdrdolst.Text);
cmd.Parameters.AddWithValue("@Post", postbox.Text);
//Check if there's a file or not
if (FileUpload1.HasFile && FileUpload1.PostedFile.ContentLength > 0)
{
FileUpload1.SaveAs(Server.MapPath("~/images/users/" + FileUpload1.FileName));
cmd.Parameters.AddWithValue("@Img", FileUpload1.FileName);
}
else
{
//Must add the paramater value to any value or DBNull.Value
cmd.Parameters.AddWithValue("@Img", "");
}
//Open the connection and execute the command
cn.Open();
cmd.ExecuteNonQuery();
//Close the connection
cn.Close();
Response.Redirect("User panel.aspx");
}
protected void countrdrdolst_SelectedIndexChanged(object sender, EventArgs e)
{
int countryID = Convert.ToInt32(countrdrdolst.SelectedValue);
DataTable StateDT = new DataTable();
using (SqlConnection con2 = new SqlConnection(ConfigurationManager.ConnectionStrings["BeravaConnectionString"].ConnectionString))
{
SqlDataAdapter adaptar = new SqlDataAdapter("Select [StateID],[State] FROM [State] WHERE [countryID] = " + countryID, con2);
adaptar.Fill(StateDT);
statedrdolst.DataSource = StateDT;
statedrdolst.DataTextField = "State";
statedrdolst.DataValueField = "StateID";
statedrdolst.DataBind();
}
statedrdolst.Items.Insert(0, new ListItem("Select State", "0"));
}
protected void statedrdolst_SelectedIndexChanged(object sender, EventArgs e)
{
int StateID = Convert.ToInt32(statedrdolst.SelectedValue);
DataTable Citydt = new DataTable();
using (SqlConnection con3 = new SqlConnection(ConfigurationManager.ConnectionStrings["BeravaConnectionString"].ConnectionString))
{
SqlDataAdapter adaptar = new SqlDataAdapter("select [CityID],[City] FROM [Cities] WHERE [StateID] = " + StateID, con3);
adaptar.Fill(Citydt);
citiesdrdolst.DataSource = Citydt;
citiesdrdolst.DataTextField = "City";
citiesdrdolst.DataValueField = "CityID";
citiesdrdolst.DataBind();
}
citiesdrdolst.Items.Insert(0, new ListItem("Select City", "0"));
}
protected void citiesdrdolst_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
答案 0 :(得分:0)
dropdownlist.Text获取下拉列表控件的SelectedValue属性。
我认为dropdownlist.SelectedItem.Text应该为您提供该国家/地区的名称,但我暂时无法检查。
但是,在userinfo表中存储ID而不是名称可能更好。如果某个州的名称更改,例如城市的名称可以拼写多种方式,该怎么办?