اعتبار سنجی مقادیر داخل GridView در حالت ویرایش در Asp.Net
دوشنبه 3 فروردین 1394در این مقاله نحوه اعتبارسنجی مقادیر داخل GridView در حالت ویرایش با استفاده از دستورات #C و VB.Net را شرح میدهیم .
هنگامی که کاربر یک سطر را بخواهد ویرایش کند می توانیم کاربر را محدود کنیم که تمام قسمتهای مربوط به آن سطر را حتمی تکمیل نماید , به صورت پیش فرض هنگامی که بخواهیم یک سطر از کنترل GridView را ویرایش کنیم , بر روی Textbox های موجود validator خاصی موجود نمی باشد .
ابتدا تگ مربوط به کنترل GridView را به صفحه اضافه می کنیم :
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCreated = "OnRowCreated" OnRowEditing="OnRowEditing">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
<asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID = "lnkEdit" Text="Edit" runat="server" CommandName="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID = "lnkUpdate" Text="Update" runat="server" OnClick="OnUpdate" />
<asp:LinkButton ID = "lnkCancel" Text="Cancel" runat="server" OnClick="OnCancel" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

سپس namespace های زیر را در قسمت codebehind صفحه اضافه میکنیم :
کد #C :
using System.Data; using System.Drawing;
کد VB.NET :
Imports System.Data Imports System.Drawing
داخل متد Pageload یک DataTable موقت میسازیم , سپس چند ستون اضافه میکنیم و داخل ستون ها را به صورت پیش فرض اطلاعاتی را تکمیل مینماییم , سپس اطلاعات داخل ستون های DataTable را در ViewState ذخیره میکنیم و در آخر کنترل GridView را به DataTable ایجاد شده متصل میکنیم :
کد #C :
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Family") });
dt.Rows.Add(1, "ایمان", "مدائنی");
dt.Rows.Add(2, " سجاد ", "باقرزاده");
dt.Rows.Add(3, " مسعود", "شریفی");
dt.Rows.Add(4, " پاکان", "رحمانی");
dt.Rows.Add(5, " احسان", "حسینی");
ViewState["dt"] = dt;
this.BindGrid();
}
}
کد VB.Net :
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id"), New DataColumn("Name"), New DataColumn("family")})
dt.Rows.Add(1, "ایمان", "مدائنی")
dt.Rows.Add(2, " سجاد ", "باقرزاده")
dt.Rows.Add(3, " مسعود", "شریفی")
dt.Rows.Add(4, " پاکان", "رحمانی")
dt.Rows.Add(5, " احسان", "حسینی")
ViewState("dt") = dt
Me.BindGrid()
End If
End Sub
در داخل رویدا OnRowEditing مربوط به GridView که هنگامی فعال میشود که کاربر دکمه داخل هر سطر را کلیک می کند , کد زیر را مینویسیم :
کد #C :
protected void OnRowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
this.BindGrid();
}
کد VB.NET :
Protected Sub OnRowEditing(sender As Object, e As GridViewEditEventArgs)
GridView1.EditIndex = e.NewEditIndex
Me.BindGrid()
End Sub
و در داخل رویداد OnRowCreated , اگر GridView ویرایش شده بود , مجدداً تمام فیلدهای سطر انتخاب شده داخل loop قرار می گیرند .داخل loop چک میشود که مقدار فیلد textbox است و اگر textbox بود , سپس محتوای داخل textbox با ID سطر انتخاب شده که در CodeBehind تعریف کرده ایم اختصاص داده میشود .
کد #C :
protected void OnRowCreated(object sender, GridViewRowEventArgs e)
{
//Find the Row being edited.
if (GridView1.EditIndex == e.Row.RowIndex)
{
//Loop through all Cells in the Row.
int index = 0;
foreach (TableCell cell in e.Row.Cells)
{
if (cell.Controls.Count > 0)
{
//Check whether Cell has TextBox.
if (cell.Controls[0] is TextBox)
{
//Reference the TextBox.
TextBox textBox = cell.Controls[0] as TextBox;
//Assign ID to the TextBox.
textBox.ID = "txt" + GridView1.HeaderRow.Cells[index].Text.Replace(" ", "");
//Create and assign RequiredFieldValidator to the TextBox.
RequiredFieldValidator validator = new RequiredFieldValidator
{
ControlToValidate = textBox.ID,
ForeColor = Color.Red,
ErrorMessage = "Required",
ValidationGroup = "Update"
};
cell.Controls.Add(validator);
}
else
{
//Add ValidationGroup to the Update Button.
LinkButton lnkUpdate = cell.FindControl("lnkUpdate") as LinkButton;
lnkUpdate.ValidationGroup = "Update";
}
}
index++;
}
}
}
کد VB.NET :
Protected Sub OnRowCreated(sender As Object, e As GridViewRowEventArgs)
'Find the Row being edited.
If GridView1.EditIndex = e.Row.RowIndex Then
'Loop through all Cells in the Row.
Dim index As Integer = 0
For Each cell As TableCell In e.Row.Cells
If cell.Controls.Count > 0 Then
'Check whether Cell has TextBox.
If TypeOf cell.Controls(0) Is TextBox Then
'Reference the TextBox.
Dim textBox As TextBox = TryCast(cell.Controls(0), TextBox)
'Assign ID to the TextBox.
textBox.ID = "txt" + GridView1.HeaderRow.Cells(index).Text.Replace(" ", "")
'Create and assign RequiredFieldValidator to the TextBox.
Dim validator As New RequiredFieldValidator() With { _
.ControlToValidate = textBox.ID, _
.ForeColor = Color.Red, _
.ErrorMessage = "Required", _
.ValidationGroup = "Update" _
}
cell.Controls.Add(validator)
Else
'Add ValidationGroup to the Update Button.
Dim lnkUpdate As LinkButton = TryCast(cell.FindControl("lnkUpdate"), LinkButton)
lnkUpdate.ValidationGroup = "Update"
End If
End If
index += 1
Next
End If
End Sub
برای دکمه Update و Cancel در حالت ویرایش نیز دستورات زیر را اضافه می کنیم :
کد #C :
protected void OnUpdate(object sender, EventArgs e)
{
GridViewRow row = (sender as LinkButton).NamingContainer as GridViewRow;
string name = (row.Cells[0].Controls[0] as TextBox).Text;
string country = (row.Cells[1].Controls[0] as TextBox).Text;
DataTable dt = ViewState["dt"] as DataTable;
dt.Rows[row.RowIndex]["Name"] = name;
dt.Rows[row.RowIndex]["Country"] = country;
ViewState["dt"] = dt;
GridView1.EditIndex = -1;
this.BindGrid();
}
protected void OnCancel(object sender, EventArgs e)
{
GridView1.EditIndex = -1;
this.BindGrid();
}
کد VB.NET :
Protected Sub OnUpdate(sender As Object, e As EventArgs)
Dim row As GridViewRow = TryCast(TryCast(sender, LinkButton).NamingContainer, GridViewRow)
Dim name As String = TryCast(row.Cells(0).Controls(0), TextBox).Text
Dim country As String = TryCast(row.Cells(1).Controls(0), TextBox).Text
Dim dt As DataTable = TryCast(ViewState("dt"), DataTable)
dt.Rows(row.RowIndex)("Name") = name
dt.Rows(row.RowIndex)("Country") = country
ViewState("dt") = dt
GridView1.EditIndex = -1
Me.BindGrid()
End Sub
Protected Sub OnCancel(sender As Object, e As EventArgs)
GridView1.EditIndex = -1
Me.BindGrid()
End Sub
- ASP.net
- 3k بازدید
- 6 تشکر