一些组合框在选中后相互重置

时间:2016-02-05 09:52:30

标签: php forms select input onchange

我有一些带有onchange事件的组合框,当他们选择其中一个时,他们会相互重置,有没有建议如何保留页面上的值?这是我的剧本:

if (imgUpload.HasFiles)
{
    try
    {
        foreach (HttpPostedFile file in imgUpload.PostedFiles)
        {
            using (Stream fs = file.InputStream)
            {
                string fileType = file.ContentType;
                if (fileType == "image/jpeg" || fileType == "image/jpg" || fileType == "image/gif" || fileType == "image/png" || fileType == "image/bmp")

                {
                    int fileSize = file.ContentLength;
                    if (fileSize <= 10485760) //Max Size: 10MB
                    {
                        Session["imgUpload"] = imgUpload;
                        err = true;
                    }
                    else
                    {
                        lblStatus.Text = "<b style='color:red'>Please select a file of max size 2MB only.</b>";
                        err = false;
                    }
                }
                else
                {
                    lblStatus.Text = "<b style='color:red'>Please select an image file only (Ex:*.jpg/*.jpeg/*.gif/*.png/*.bmp)</b>";
                    err = false;
                }
            }
        }
    }
    catch (Exception)
    {
        lblStatus.Text = "<b style='color:red'>Some problem occurred while uploading the file. Please try after some time.</b>";
        err = false;
    }
}

protected void imgUpload2()
{
    objImage = new ImageModel();
    objOperation = new Operations();
    if (Session["imgUpload"] != null && (!imgUpload.HasFile))
    {
        imgUpload = (FileUpload)Session["imgUpload"];
    }
    //else if (imgUpload.HasFile)
    //{
    //    Session["FileUpload1"] = imgUpload;
    //}

        if (imgUpload.PostedFiles.Count > 0)
    {
        foreach (HttpPostedFile file in imgUpload.PostedFiles)
        {
            using (Stream fs = file.InputStream)
            {
                lblStatus.Visible = false;
                using (BinaryReader br = new BinaryReader(fs))
                {
                    byte[] bytes = br.ReadBytes((Int32)fs.Length);
                    objImage.ImageName = file.FileName;
                    objImage.ImageInByte = bytes;
                    var rs = objOperation.insertImage(objImage);
                }
            }
        }
    }
}

public int insertImage(ImageModel objImage)
        {
            var result = MySqlHelper.ExecuteNonQuery(Common.GetConnection(), CommandType.StoredProcedure, ConstantFields.StoredProcedureClass.sp_insertImage,
                new MySqlParameter("image_name",objImage.ImageName),
                new MySqlParameter("image_byteImage", objImage.ImageInByte)              
                );
            return result;
        }

Stored Procedure:
BEGIN
set @ad_id:=(select max(ad_id) from postadverties p,user_register u 
where p.ad_userid=u.user_id);
insert into images(image_name,image_byteImage,image_adid)
values(image_name,image_byteImage,@ad_id);
END

希望有任何建议可以解决我的问题,

1 个答案:

答案 0 :(得分:0)

onchange="submit(this)" 表示您希望在组合框的值更改时提交表单。因此,在发送表单时,页面会重新加载并获得表单的默认值。 要恢复所选的值,我会做类似的事情:

<select name="select_kabkota" style="width:18px;" onchange="submit(this)">  
<option>KAB/KOTA</option>
  <?php
  if(isset($_POST['select_kabkota']))
    $select_kabkota=$_POST['select_kabkota'];

  while($row_kabkota=$result_kabkota->fetch_assoc())
  {
    $selected = $select_kabkota == $row_kabkota['kab_kota'] ? 'selected="selected"' : '';
    echo "<option ".$selected." >".$row_kabkota['kab_kota']."</option>";
  }
?>
</select>