Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取名称而不是ID编号_C#_Asp.net_Sql_Ajax - Fatal编程技术网

C# 获取名称而不是ID编号

C# 获取名称而不是ID编号,c#,asp.net,sql,ajax,C#,Asp.net,Sql,Ajax,我有一个问题,因为我已经创建了三个级联的dropdownlist,其中显示了国家、州和城市,但我想保存dropdownlist显示的内容,如果您单击下面的链接,您可以获得更多信息,因为我已经记录了我的情况。谢谢大家的支持 ASP.NET标记 <div class="banner728"> <asp:Image ID="Image1" runat="server" ImageUrl="~/images/general/bann72890.png" /&

我有一个问题,因为我已经创建了三个级联的dropdownlist,其中显示了国家、州和城市,但我想保存dropdownlist显示的内容,如果您单击下面的链接,您可以获得更多信息,因为我已经记录了我的情况。谢谢大家的支持

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"> &nbsp; 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">
            &nbsp; 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">
            &nbsp;
        </td>
    </tr>

    <tr>
        <td class="style5">
            &nbsp; 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">
            &nbsp;
        </td>
    </tr>

    <tr>
        <td class="style5">
            &nbsp; 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">
            &nbsp; Å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">
            &nbsp; 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>
            &nbsp;
        </td>
    </tr>
    <tr>
        <td class="style5">
            &nbsp; 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">
            &nbsp;</td>
    </tr>
    <tr>
        <td class="style5">
            &nbsp; 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">
            &nbsp;</td>
    </tr>
    <tr>
        <td class="style5">
            &nbsp; 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">
            &nbsp; 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">
            &nbsp;
        </td>
    </tr>
    <tr>
        <td class="style5">
            &nbsp; Personlig bild
        </td>
        <td style="text-align: left" class="style8">
            <asp:FileUpload ID="FileUpload1" runat="server" />
        </td>
        <td style="text-align: left">
            &nbsp;
        </td>
    </tr>
   <tr>
   <td>


   </tr>
    <tr>
        <td class="style5">
            &nbsp;
            </td>
        <td class="style8">







            <asp:Button ID="btnSave" runat="server" onclick="btnSave_Click" Text="Create" CausesValidation="True"/>







        </td>
        <td>
            &nbsp;</td>
    </tr>
</table>
<br />
<br />
<br />
</ContentTemplate>
</asp:UpdatePanel>

Text获取dropdownlist控件的SelectedValue属性

我想dropdownlist.SelectedItem.Text应该告诉你国家的名称,但我现在无法查看


但是,在userinfo表中存储ID可能比存储名称更好。例如,如果一个州的名称发生了变化,或者一个城市可以用多种方式拼写,该怎么办?

感谢您所做的大量工作,因为您提到了我必须添加的内容。SelectedItem.Text再次感谢:-))
   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)
    {

    }

    }
}