|
|
|
ASP.Net รบกวนเกี่ยวกับการใช้ Gridview Edit หน่อย ครับ CommandField ShowEditButton="true" |
|
|
|
|
|
|
|
คือผมจะช้ Gridview ในการแก้ไขข้อมูลทั้งแถวอ่ะครับ แต่ลองมาใช้ตัว CommandField ของ Gridview ดู แต่ไม่รู้ต้องใส่โค้ดอะไรไปต่ออะครับ ผมต้องเพิ่ม Event ตัวไหนเข้าไปอะคับ รบกวนหน่อย
คือในส่วนของหน้า Gridview นี้ โชว์ข้อมูลขึ้นมาแค่ 5 ฟิว แต่พอถ้า คลิก Edit ปุีบจะส่งค่าไปอีกหน้านึงซึ่งมีทั้งหมด 50 ฟิว
.aspx
<asp:GridView ID="gvStaff" runat="server" Visible="False"
AutoGenerateColumns="False" Width="100%"
BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px"
CellPadding="2" ForeColor="Black" GridLines="None" AllowPaging="True"
onpageindexchanging="gvStaff_PageIndexChanging" PageSize="20"
onrowcommand="gvGrid_Command">
<AlternatingRowStyle BackColor="PaleGoldenrod" />
<FooterStyle BackColor="Tan" />
<HeaderStyle BackColor="Tan" Font-Bold="True" />
<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue"
HorizontalAlign="Center" />
<SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
<SortedAscendingCellStyle BackColor="#FAFAE7" />
<SortedAscendingHeaderStyle BackColor="#DAC09E" />
<SortedDescendingCellStyle BackColor="#E1DB9C" />
<SortedDescendingHeaderStyle BackColor="#C2A47B" />
<Columns>
<asp:BoundField DataField ="CITIZEN_ID" HeaderText="CITIZEN_ID" SortExpression="CITIZEN_ID" ItemStyle-HorizontalAlign="Center" />
<asp:BoundField DataField ="STF_FNAME" HeaderText="Name" SortExpression="STF_FNAME" ItemStyle-HorizontalAlign="Center" />
<asp:BoundField DataField ="STF_LNAME" HeaderText="Lastname" SortExpression="STF_LNAME" ItemStyle-HorizontalAlign="Center" />
<asp:BoundField DataField ="BIRTHDAY" HeaderText="Birthday" SortExpression="BIRTHDAY" ItemStyle-HorizontalAlign="Center" />
<asp:BoundField DataField ="TELEPHONE" HeaderText="Tel" SortExpression="TELEPHONE" ItemStyle-HorizontalAlign="Center" />
<asp:CommandField ShowEditButton="true" ButtonType="Link" HeaderStyle-Wrap="true" EditText="Edit" />
</Columns>
</asp:GridView>
.aspx.cs
protected void btnType_Click(object sender, EventArgs e)
{
if(ddlType.SelectedValue == "1")
{
gvStaff.Visible = true;
gvStudent.Visible = false;
BindData_gvStaff();
}
else if (ddlType.SelectedValue == "2")
{
gvStaff.Visible = false;
gvStudent.Visible = true;
BindData_gvStudent();
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "ErrorAlert", "alert('กรุณาเลือกประเภทข้อมูลที่ต้องการโชว์');", true);
gvStaff.Visible = false;
gvStaff.DataSource = null;
gvStaff.DataBind();
gvStudent.Visible = false;
gvStudent.DataSource = null;
gvStudent.DataBind();
}
}
protected void BindData_gvStaff()
{
String strSQL;
strSQL = "SELECT CITIZEN_ID,STF_FNAME,STF_LNAME,BIRTHDAY,TELEPHONE FROM UOC_STAFF ";
clsDB.QueryDataTable(strSQL);
gvStaff.DataSource = clsDB.QueryDataTable(strSQL);
gvStaff.DataBind();
}
protected void BindData_gvStudent()
{
String strSQL;
strSQL = "SELECT STUDENTCODE,CITIZEN_ID,STD_FNAME,STD_LNAME,BIRTHDAY FROM UOC_STUDENT ";
clsDB.QueryDataTable(strSQL);
gvStudent.DataSource = clsDB.QueryDataTable(strSQL);
gvStudent.DataBind();
}
protected void gvStaff_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
String strSQL;
strSQL = "SELECT CITIZEN_ID,STF_FNAME,STF_LNAME,BIRTHDAY,TELEPHONE FROM UOC_STAFF";
clsDB.QueryDataTable(strSQL);
gvStaff.PageIndex = e.NewPageIndex;
gvStaff.DataSource = clsDB.QueryDataTable(strSQL);
gvStaff.DataBind();
}
protected void gvStudent_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
String strSQL;
strSQL = "SELECT STUDENTCODE,CITIZEN_ID,STD_FNAME,STD_LNAME,BIRTHDAY FROM UOC_STUDENT ";
clsDB.QueryDataTable(strSQL);
gvStudent.PageIndex = e.NewPageIndex;
gvStudent.DataSource = clsDB.QueryDataTable(strSQL);
gvStudent.DataBind();
}
Tag : .NET, Web (ASP.NET), C#, VS 2010 (.NET 4.x)
|
|
|
|
|
|
Date :
2013-09-04 15:31:18 |
By :
offonepoint |
View :
1746 |
Reply :
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ถ้าจะใช้ gridview แบบ edit ได้
ต้องใช้ template field ไม่ใช่ bound field
|
|
|
|
|
Date :
2013-09-05 08:01:20 |
By :
ห้ามตอบเกินวันละ 2 กระทู้ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ได้แล้วครับ
Code (C#)
<asp:GridView ID="gvStaff" runat="server" Visible="False"
AutoGenerateColumns="False" Width="100%"
BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px"
CellPadding="2" ForeColor="Black" GridLines="None" AllowPaging="True"
onpageindexchanging="gvStaff_PageIndexChanging" PageSize="20"
onrowdatabound="gvStaff_DataBound" >
<AlternatingRowStyle BackColor="PaleGoldenrod" />
<FooterStyle BackColor="Tan" />
<HeaderStyle BackColor="Tan" Font-Bold="True" />
<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue"
HorizontalAlign="Center" />
<SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
<SortedAscendingCellStyle BackColor="#FAFAE7" />
<SortedAscendingHeaderStyle BackColor="#DAC09E" />
<SortedDescendingCellStyle BackColor="#E1DB9C" />
<SortedDescendingHeaderStyle BackColor="#C2A47B" />
<Columns>
<asp:BoundField DataField ="CITIZEN_ID" HeaderText="CITIZEN_ID"
SortExpression="CITIZEN_ID" ItemStyle-HorizontalAlign="Center" >
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField ="STF_FNAME" HeaderText="Name"
SortExpression="STF_FNAME" ItemStyle-HorizontalAlign="Center" >
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField ="STF_LNAME" HeaderText="Lastname"
SortExpression="STF_LNAME" ItemStyle-HorizontalAlign="Center" >
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField ="BIRTHDAY" HeaderText="Birthday"
SortExpression="BIRTHDAY" ItemStyle-HorizontalAlign="Center" >
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField ="TELEPHONE" HeaderText="Tel"
SortExpression="TELEPHONE" ItemStyle-HorizontalAlign="Center" >
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundField>
<asp:TemplateField>
<ItemTemplate>
<asp:Hyperlink id="hplEdit" runat="server">Edit</asp:Hyperlink>
</ItemTemplate>
</asp:TemplateField>
Code (C#)
protected void gvStaff_DataBound(object sender, GridViewRowEventArgs e)
{
HyperLink hplEdit = (HyperLink)(e.Row.FindControl("hplEdit"));
if(hplEdit != null)
{
hplEdit.Text = "Edit";
hplEdit.NavigateUrl = "It_Admin_Edit_Form_Staff.aspx?CITIZEN_ID=" + (string)DataBinder.Eval(e.Row.DataItem, "CITIZEN_ID");
}
}
|
|
|
|
|
Date :
2013-09-05 11:59:31 |
By :
offonepoint |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|