简体   繁体   中英

Dropdown List Select index Default if no User Input

I have a dropdown list which works fine if the user selects one of the 6 options. However if no action is taken because the first Option is the one required the selected value stays blank.

I have tried to set default selected value and search other solutions via Stack Overflow. Code Project, etc. but nothing works. it may be something basic in my code!

    static string prevPage = String.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        FileUpload1.Attributes["multiple"] = "multiple";

        txtUN.Text = Request.QueryString["SVCCNo"];
        lblid.Text = Session["username"].ToString();
        txtCID.Text = Request.QueryString["CID"];
        lblCID.Text = Request.QueryString["CID"];
        lblSeparator.Text = " - ";
        lblLocation.Text = Request.QueryString["LName"];
        lblAssetName.Text = Request.QueryString["SVCCIDName"];

        if (!IsPostBack)
        {
            prevPage = Request.UrlReferrer.ToString();
        }
    }

    protected void Upload(object sender, EventArgs e)
    {
        string conn = ConfigurationManager.ConnectionStrings["SVCCAssetsDb"].ConnectionString;
        SqlConnection sqlcon = new SqlConnection(conn);
        sqlcon.Open();
        //lblType.Text = "1";

        for (int i = 0; i < Request.Files.Count; i++)
        {
            //move lbl inside loop
            int uniquenuumber = Convert.ToInt32(txtUN.Text);

            HttpPostedFile postedFile = Request.Files[i];
            if (postedFile.ContentLength > 0)
            {
                //lblType.Text = txtType.Text;
                int txttype = 1;
                txttype = Convert.ToInt32(lblType.Text);
                string userid = lblid.Text;
                string fileName = Path.GetFileName(postedFile.FileName);
                postedFile.SaveAs(Server.MapPath("~/Attachment/") + fileName);
                lblMessage.Text += string.Format("<b>{0}</b> uploaded.<br />", fileName);

                string sqlquery = "INSERT INTO Attachment (UserName, FilePath, UniqueNumber, TypeCode) VALUES ('" + userid + "', + '" + fileName + "', + '" + uniquenuumber + "', '" + txttype + "')";
                SqlCommand sqlcmd = new SqlCommand(sqlquery, sqlcon);

                sqlcmd.ExecuteNonQuery();
            }
        }
        sqlcon.Close();
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        var selectedValue = ((DropDownList)sender).SelectedValue;
        if (!string.IsNullOrEmpty(txtType.Text))
        txtType.Text = selectedValue;
        lblType.Text = txtType.Text;
    }

    protected void btnReturn_Click(object sender, EventArgs e)
    {
        Response.Redirect(prevPage);
    }
}

aspx code:

Multi-File Upload

Files Must be All of the Same Type eg Photographs



Select the File Type Before Selecting the Files to Upload
Otherwise Files Chosen will be Clear Cleared

From what I guess without having the front code :

You're having a problem with this line :

int txttype = 1;
txttype = Convert.ToInt32(lblType.Text);

Which is caused because if you don't select another item from your dropdown, your label isn't updated and so this create a problem ?

If that's the case (else just explain me where I'm wrong )

You could just do a first intialisation of your label Type at the page load (in the !Page.IsPostBack Condition !!)

lblType.Text = "Your Default value as string";

Indeed, the select index changed event will trigger only if you change the dropdown, which in the case, if you stay on the first item, will not be triggered.

But there's maybe something else, let's try that first ! :)

 SOLVED! Just a matter of getting the Code Rejigged.
 Code Now Reads:
        string conn = 
        ConfigurationManager.ConnectionStrings["SVCCAssetsDb"].ConnectionString;
        SqlConnection sqlcon = new SqlConnection(conn);
        sqlcon.Open();
        int txttype = Convert.ToInt32(lblType.Text);

        for (int i = 0; i < Request.Files.Count; i++)
        {
            //move lbl inside loop
            int uniquenuumber = Convert.ToInt32(txtUN.Text);

            HttpPostedFile postedFile = Request.Files[i];
            if (postedFile.ContentLength > 0)
            {
                //lblType.Text = txtType.Text;
                //int txttype = 1;
                txttype = Convert.ToInt32(lblType.Text);
                string userid = lblid.Text;
                string fileName = Path.GetFileName(postedFile.FileName);
                postedFile.SaveAs(Server.MapPath("~/Attachment/") + fileName);
                lblMessage.Text += string.Format("<b>{0}</b> uploaded.<br />", 
    fileName);

                string sqlquery = "INSERT INTO Attachment (UserName, FilePath, 
    UniqueNumber, TypeCode) VALUES ('" + userid + "', + '" + fileName + "', + '" + 
   uniquenuumber + "', '" + txttype + "')";
                SqlCommand sqlcmd = new SqlCommand(sqlquery, sqlcon);

                sqlcmd.ExecuteNonQuery();
            }
        }
        sqlcon.Close();
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        var selectedValue = ((DropDownList)sender).SelectedValue;
        if (!string.IsNullOrEmpty(txtType.Text))
        txtType.Text = selectedValue;
        lblType.Text = selectedValue;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM