3月 28, 2014

在GridView做加減並更新

 protected void gv_DataBound(object sender, EventArgs e)
    {
        for (int i = 0; i < gv.Rows.Count; i++)
        {
            Label aa = (Label)gv.Rows[i].Cells[6].FindControl("Label3");
            
            if (aa != null)
            {
                if (aa.Text.Trim().StartsWith("-")) aa.Text = "0";
                int number;
                string hu = aa.Text.Trim();
                int.TryParse(hu, out number);
                int num2 = int.Parse(DropDownList1.SelectedValue);
                int num3 = number * num2;
                gv.Rows[i].Cells[7].Text = num3.ToString();
            }
            
        }


    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlDataSource ses = new SqlDataSource();
        ses.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["immis"].ConnectionString;
        ses.UpdateCommand = "update hi set total=@total where id=101";
        ses.UpdateParameters.Add(new Parameter("total", TypeCode.String));
        ses.UpdateParameters["total"].DefaultValue = DropDownList1.SelectedValue.ToString();
        ses.Update();

    }

3月 27, 2014

MVC 進階WebGrid的用法

首先在VIEW前面宣告如下
@using mvc3.Models;
@{
    ViewBag.Title = "index";
    Model aa = new Model();
    var grid = new WebGrid(aa.gridv(), canPage: true, rowsPerPage: 15,
selectionFieldName: "selectedRow", ajaxUpdateContainerId: "gridContent");
}
接著是在需要呈現的HTML如下
<div id="gridContent">
@grid.GetHtml(
columns: grid.Columns(
grid.Column("id", "id"),
grid.Column("item", "item"),
grid.Column("name", "改字", style: "description")
)) 
</div>
在MODEL的程式碼如下
public class data
        {
            public string id { get; set; }
            public string item { get; set; }
            public string name { get; set; }
        }
        public List<data> gridv()
        {
            string a = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
            SqlConnection conn = new SqlConnection(a);
            conn.Open();
            string sql = "select [id],[item],[cs] from item";
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataReader dr = cmd.ExecuteReader();
            List<data> oop = new List<data>();
            while(dr.Read())
            {            
                oop.Add(new data()
                {
                    id = dr[0].ToString(),
                    item = dr[1].ToString(),
                    name = dr[2].ToString()
                });
            }
            dr.Close();
            dr.Dispose();
            cmd.Dispose();
            conn.Close();
            conn.Dispose();
            return oop;
        }
不用WebGrid的方法如下
@foreach (var d in aa.gridv()){
      <table>
          <tr><td>@d.id</td><td>@d.item</td><td>@d.name</td></tr>
     </table>
}

3月 24, 2014

MVC 高階連動式下拉選單DropDownList

首先在View新增兩個下拉選單
@Html.DropDownList("select2",null ,new { id="Select2"})
@Html.DropDownList("select3",null ,new { id="Select3"})
根據上一篇的結果 我們會在Controll中把縣市的資料塞給Select2

        Model ty = new Model();  
        public ActionResult Index()
        {
            
            ViewBag.select2 = ty.see();
            ViewBag.select3 = ty.see3("3");
            return View();
        }
然後因為Select2有預設選項為3 所以我們在ty.see3(3)就給3 然後進Model開始寫see3()的方法如下
public List<SelectListItem> see3(string city)
        {
            string a = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
            SqlConnection conn = new SqlConnection(a);
            conn.Open();
            string sql = "select area , aid from area where id=@id";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.Add("@id", System.Data.SqlDbType.Int).Value = city;
            SqlDataAdapter da = new SqlDataAdapter(sql,conn);
            DataTable dt = new DataTable();
            da.SelectCommand = cmd;
            da.Fill(dt);
            List<SelectListItem> yuii = new List<SelectListItem>();
            for (int i = 0; i < dt.Rows.Count;i++ )
            {
                yuii.Add(new SelectListItem()
                {
                    Text = dt.Rows[i].ItemArray[0].ToString(),
                    Value = dt.Rows[i].ItemArray[1].ToString()
                    
                });
            }
            da.Dispose();
            dt.Dispose();
            cmd.Dispose();
            conn.Close();
            conn.Dispose();
            return yuii;
        }
接著回到Controll另外寫個ActionResult:
public ActionResult details(string user)
        {
            
            var sop = ty.see3(user);
            TagBuilder tb = new TagBuilder("select");
            tb.GenerateId("Select3");
            tb.MergeAttribute("name","select3");
            foreach (var item in sop)
            {
                tb.InnerHtml+="<option value='"+item.Value.ToString()+"'>"+item.Text.ToString()+"</option>";
                         
            }
            return Content(tb.ToString());
            
        }
這邊是等AJAX呼叫時馬上去跟MODEL要資料再生成HTML程式碼return回去 記得ID要一樣,然後接著是前端的Jquery寫法如下
$("#Select2").change(function () {/*當縣市的下拉選單改變時*/
                var selected = $("#Select2 option:selected");
                $.post("@Url.Content("~/home/details")",
                        { user: selected.val() },
                        function (res) {
                            $("#Select3").replaceWith(res);
                            
                        });
                
            });
如此一來,當縣市的選單改變後,會post選取值去Controll那邊 然後controll那邊會負責要資料在return出HTML,選區域的Select3的選項就會改變!

3月 23, 2014

MVC 進階ListBox和DropDownList

首先要到Model.cs記得using System.Web.Mvc;
public List<SelectListItem> see()
        {
            string a = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
            SqlConnection conn =new SqlConnection(a);
            conn.Open();
            string sql = "spii";
            SqlCommand cmd = new SqlCommand(sql,conn);
            
            SqlDataReader dr = cmd.ExecuteReader();
            List<SelectListItem> yui = new List<SelectListItem>();
            
            
                for (int i = 0; i <=dr.FieldCount+1; i++)
                {
                    dr.Read();
                    yui.Add(new SelectListItem()
                    {
                        Text = dr[1].ToString(),
                        Value = dr[0].ToString(),
                        Selected = dr[0].Equals(3)
                    });
                }
            
            
            dr.Close();
            cmd.Dispose();
            conn.Close();
            conn.Dispose();
            return yui;
            
        }

接著利用List將資料return
回到Controll那邊:
Model ty = new Model();
public ActionResult Index()
{
    ViewBag.select1 = ty.see();
    ViewBag.select2 = ty.see();
}

再回到View:
@Html.ListBox("select1", null, new { id="SelectList2",size="2"})
@Html.DropDownList("select2",null ,new { id="Select2"})
大功告成!!

3月 22, 2014

MVC 淺談ListBox和DropDownList

純靜態ListBox No Value:
<select id="SelectList1" multiple="multiple" name="uu" size="2">
        <option>hello</option>
        <option selected>apple</option>
        <option>listbox</option>
        <option>All</option>
    </select>
    @Html.ListBox("select1", null, new { id="SelectList2",size="2"})
塞值方法如下:
進該頁的Controll:
public ActionResult Index()
        {
            
            string[] yy = new string[2];
            yy[0] = "1";
            yy[1] = "2";
            
            SelectList LB = new SelectList(yy);
            ViewBag.select1 = LB;
            

            return View();
        }

純靜態DropDownList No Value:

    <select id="Select1">
        <option>dog</option>
        <option>cat</option>
    </select>
    @Html.DropDownList("select2",null ,new { id="Select2"})

塞值方法如下:
進該頁的Controll:
public ActionResult Index()
        {
            string[] gg = new string[2];
            gg[0] = "HIGH";
            gg[1] = "LOW";
            
            SelectList DDL = new SelectList(gg);
            ViewBag.select2 = DDL;
            
           

            return View();
        }