//在資料FormView完成資料繫結後存取其內容資料 protected void fvEmp_DataBound(object sender, EventArgs e) { if (fvEmp.CurrentMode == FormViewMode.ReadOnly) { //透過DataRowView來讀取資料欄位,這方法也適用於DetailsView DataRowView drView = (DataRowView)fvEmp.DataItem; txtMsg1.Text = "<Font Color='Red'>這是DataRowView</Font><br/>"; txtMsg1.Text += drView["EmployeeID"].ToString() + "<br/>"; txtMsg1.Text += drView["LastName"].ToString() + "<br/>"; txtMsg1.Text += drView["FirstName"].ToString() + "<br/>"; txtMsg1.Text += drView["City"].ToString() + "<br/>"; txtMsg1.Text += drView["Country"].ToString() + "<br/>"; //透過尋找Label控制項的方式來達成 FormViewRow Row = fvEmp.Row; txtMsg2.Text = "<Font Color='Red'>這是FormViewRow</Font><br/>"; txtMsg2.Text += ((Label)Row.FindControl("txtEmployeeID")).Text + "<br/>"; txtMsg2.Text += ((Label)Row.FindControl("txtLastName")).Text + "<br/>"; txtMsg2.Text += ((Label)Row.FindControl("txtFirstName")).Text + "<br/>"; txtMsg2.Text += ((Label)Row.FindControl("txtCity")).Text + "<br/>"; txtMsg2.Text += ((Label)Row.FindControl("txtCountry")).Text + "<br/>"; } AddPagerIndex(); //加入Page索引 }
protected void fv_OfficerPosition_ItemInserting(object sender, FormViewInsertEventArgs e) { FormViewRow row = ((FormView)sender).Row; // UrlControl has limited separation of concerns and allows many variations to come out of the Url property // in this case, we have specified that the there has to be a file chosen from the DNN system. // UrlControl will use the FileID={fileid} output. We are only interested in the actual file id. UrlControl urlOfficeBadge = row.FindControl("urlOfficeBadge") as UrlControl; if (urlOfficeBadge != null) { if (urlOfficeBadge.Url.StartsWith("FileID=")) { int fileid; if (int.TryParse(urlOfficeBadge.Url.Substring(7), out fileid)) { e.Values["FileId"] = fileid; } } else if (urlOfficeBadge.Url.Equals(String.Empty)) { e.Values["FileId"] = null; } } }
protected void FormView1_ItemCommand(object sender, FormViewCommandEventArgs e) { if (e.CommandName == "Đặt món") { FormViewRow row = FormView1.Row; string ma = ((Label)row.FindControl("mamonanLabel1")).Text; //string ma = "M103"; decimal gia = Decimal.Parse(((Label)row.FindControl("giaLabel")).Text); string ten = ((Label)row.FindControl("tenmonanLabel")).Text; ShoppingCart acart; if (Session["cart"] == null) //neu chua co gio hang { acart = new ShoppingCart(); //tao gio hang moi } else { acart = (ShoppingCart)Session["cart"]; } //them vao gio hang acart.InsertItem(ma, ten, gia, 1); //dat lai vao session Session["cart"] = acart; //chuyen den trang gio hang Response.Redirect("Cart.aspx"); } }
//建立及取得FormView的HeaderRow及HeaderRow protected void AddPagerIndex() { //目的是為了加入Page參考索引筆數 FormViewRow headerRow = fvEmp.HeaderRow; FormViewRow footerRow = fvEmp.FooterRow; FormViewRow bottomPagerRow = fvEmp.BottomPagerRow; headerRow.BackColor = Color.Red; footerRow.BackColor = Color.LightBlue; Label txtPagerNo1 = new Label(); Label txtPagerNo2 = new Label(); txtPagerNo1.Text = "員工基本資料維護( " + (fvEmp.DataItemIndex + 1) + "/" + fvEmp.DataItemCount + " )"; txtPagerNo2.Text = "資料項目索引( " + (fvEmp.DataItemIndex + 1) + "/" + fvEmp.DataItemCount + ")"; headerRow.Cells[0].Controls.Add(txtPagerNo1); bottomPagerRow.Cells[0].Controls.Add(txtPagerNo2); }
protected void formViewLang_ItemCreated(object sender, EventArgs e) { LocaleController lc = new LocaleController(); Dictionary <string, Locale> loc = lc.GetLocales(PortalSettings.Current.PortalId); FormViewRow row = formViewLang.HeaderRow; if (row != null) { PlaceHolder phLanguage = row.FindControl("phLanguage") as PlaceHolder; int i = 0; foreach (KeyValuePair <string, Locale> item in loc) { HtmlGenericControl div = new HtmlGenericControl("div"); div.Style.Add("display", "table-cell"); ImageButton imgBtn = new ImageButton(); imgBtn.ID = "imgLanguage" + i.ToString(); imgBtn.Click += Language_Selected; imgBtn.ImageUrl = "~\\images\\Flags\\" + item.Key + ".gif"; imgBtn.Style.Add("padding", "3px 10px 5px 10px"); imgBtn.Style.Add("border-width", "1px"); imgBtn.Style.Add("border-color", "#000000"); if (i == formViewLang.PageIndex) { imgBtn.Style.Add("border-style", "solid solid hidden solid"); imgBtn.Style.Add("margin", "0 0 1px 0;"); imgBtn.Style.Add("background-color", "White"); } else { imgBtn.Style.Add("border-style", "solid solid solid solid"); imgBtn.Style.Add("margin", "0"); imgBtn.Style.Add("background-color", "LightGrey"); } div.Controls.Add(imgBtn); phLanguage.Controls.Add(div); i++; } } }
protected override int CreateChildControls(System.Collections.IEnumerable dataSource, bool dataBinding) { int rows = base.CreateChildControls(dataSource, dataBinding); // no data rows created, create empty table if enabled if (rows == 0 && this.ShowWhenEmpty) { // create the table Table table = new Table(); FormViewRow fr = new FormViewRow(0, DataControlRowType.DataRow, DataControlRowState.Normal); TableCell tc = new TableCell(); this.ItemTemplate.InstantiateIn(tc); fr.Controls.Add(tc); table.Controls.Add(fr); this.Controls.Clear(); this.Controls.Add(table); } return(rows); }