更新在asp.net中的GridView时的SqlDataSource绑定 [英] Update in asp.net gridview when binding with SqlDataSource
本文介绍了更新在asp.net中的GridView时的SqlDataSource绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个与SqlDataSource1绑定在的.aspx page.But当我使用事件OnRowUpdating,并与例外。一种落后code更新发生的GridView的
更新不受数据源SqlDataSource1除非指定更新命令支持。
块引用>的GridView,我现在用的就是
< ASP:GridView控件ID =gdvProfiles=服务器AllowPaging =真AllowSorting =真的DataSourceID =SqlDataSource1
OnRowUpdating =updateRecordOnRowEditing =editRecordOnRowCancelingEdit =cancelRecord
的AutoGenerateColumns =FALSE的cellpadding =0前景色=#333333网格=无
风格=保证金权:38pxFONT-SIZE =小>和OnRowUpdaing
保护无效updateRecord(对象发件人,GridViewUpdateEventArgs E)
{
尝试
{
标签lblusername = gdvProfiles.Rows [e.RowIndex] .FindControl(lblusername)作为标签;
文本框txtName的= gdvProfiles.Rows [e.RowIndex] .FindControl(改为txtName)的文本框;
文本框txtsponser = gdvProfiles.Rows [e.RowIndex] .FindControl(txtsponser)的文本框;
文本框txtemail = gdvProfiles.Rows [e.RowIndex] .FindControl(txtemail)的文本框;
文本框txtdob = gdvProfiles.Rows [e.RowIndex] .FindControl(txtdob)的文本框; 文本框txthomecontact = gdvProfiles.Rows [e.RowIndex] .FindControl(txthomecontact)的文本框;
文本框txtcontact = gdvProfiles.Rows [e.RowIndex] .FindControl(txtcontact)的文本框;
文本框txtaddress = gdvProfiles.Rows [e.RowIndex] .FindControl(txtaddress)的文本框;
文本框txtcity = gdvProfiles.Rows [e.RowIndex] .FindControl(txtcity)的文本框;
文本框txtstate = gdvProfiles.Rows [e.RowIndex] .FindControl(txtstate)的文本框;
文本框txtzip code = gdvProfiles.Rows [e.RowIndex] .FindControl(txtzip code)的文本框;
文本框txtcountry = gdvProfiles.Rows [e.RowIndex] .FindControl(txtcountry)的文本框;
BORegistration oBORegistration =新BORegistration();
BalRegistration oBalRegistration =新BalRegistration();
oBORegistration.Name = lblusername.Text; oBORegistration.Name = txtname.Text;
oBORegistration.Sponser = txtsponser.Text;
oBORegistration.Email = txtemail.Text;
oBORegistration.Dob = txtdob.Text; oBORegistration.HomeContact = txthomecontact.Text;
oBORegistration.Contact = txtcontact.Text;
oBORegistration.Addressx = txtaddress.Text;
oBORegistration.City = txtcity.Text;
oBORegistration.State = txtstate.Text;
oBORegistration.Zip code = txtzip code.Text;
oBORegistration.Country = txtcountry.Text; 如果(oBalRegistration.Updatememberprofile(oBORegistration))
{
gdvProfiles.EditIndex = -1;
// FillData();
gdvProfiles.DataBind();
}
}
抓住
{
扔;
}
}在SqlDataSounce是
< ASP:SqlDataSource的ID =SqlDataSource1=服务器的ConnectionString =<%$的ConnectionStrings:通用%GT;
的ProviderName =System.Data.SqlClient的的SelectCommand =spGetMemberProfileSelectCommandType =StoredProcedure的
FilterEx pression =用户名LIKE'{0}%或名称,如{0}%'OR
Sponser LIKE'{0}%'或电子邮件LIKE'{0}%'OR LIKE多波{0}%'OR
首页联系LIKE'{0}%或联系LIKE'{0}%或市LIKE'{0}%'OR
Statex LIKE'{0}%或邮编code LIKE'{0}%'或国家LIKE'{0}%
>
< FilterParameters>
< ASP:ControlParameter名称=ID控件ID =txtFilter/>
< / FilterParameters>
< / ASP:SqlDataSource的>
解决方案我已经在SqlDataSqouce提到的UpdateCommand做到了这一点为
< ASP:SqlDataSource的ID =SqlDataSource1=服务器的ConnectionString =<%$的ConnectionStrings:通用%GT;
的ProviderName =System.Data.SqlClient的的SelectCommand =spGetMemberProfile的UpdateCommand =spGetMemberProfile
SelectCommandType =StoredProcedure的FilterEx pression =用户名LIKE'{0}%或名称,如{0}%'OR
Sponser LIKE'{0}%'或电子邮件LIKE'{0}%'OR LIKE多波{0}%'OR
首页联系LIKE'{0}%或联系LIKE'{0}%或市LIKE'{0}%'OR
Statex LIKE'{0}%或邮编code LIKE'{0}%'或国家LIKE'{0}%
>
< FilterParameters>
< ASP:ControlParameter名称=ID控件ID =txtFilter/>
< / FilterParameters>
< / ASP:SqlDataSource的>I have a GridView that is bind with SqlDataSource1 in .aspx page.But when I use event "OnRowUpdating" and update it with code behind .An exception occur that
Updating is not supported by data source 'SqlDataSource1' unless UpdateCommand is specified.
Gridview that I am using is
<asp:GridView ID="gdvProfiles" runat="server" AllowPaging="True" AllowSorting="True" DataSourceID="SqlDataSource1" OnRowUpdating="updateRecord" OnRowEditing="editRecord" OnRowCancelingEdit="cancelRecord" AutoGenerateColumns="False" CellPadding="0" ForeColor="#333333" GridLines="None" Style="margin-right: 38px" Font-Size="Small">
and OnRowUpdaing
protected void updateRecord(object sender, GridViewUpdateEventArgs e) { try { Label lblusername = gdvProfiles.Rows[e.RowIndex].FindControl("lblusername") as Label; TextBox txtname = gdvProfiles.Rows[e.RowIndex].FindControl("txtname") as TextBox; TextBox txtsponser = gdvProfiles.Rows[e.RowIndex].FindControl("txtsponser") as TextBox; TextBox txtemail = gdvProfiles.Rows[e.RowIndex].FindControl("txtemail") as TextBox; TextBox txtdob = gdvProfiles.Rows[e.RowIndex].FindControl("txtdob") as TextBox; TextBox txthomecontact = gdvProfiles.Rows[e.RowIndex].FindControl("txthomecontact") as TextBox; TextBox txtcontact = gdvProfiles.Rows[e.RowIndex].FindControl("txtcontact") as TextBox; TextBox txtaddress = gdvProfiles.Rows[e.RowIndex].FindControl("txtaddress") as TextBox; TextBox txtcity = gdvProfiles.Rows[e.RowIndex].FindControl("txtcity") as TextBox; TextBox txtstate = gdvProfiles.Rows[e.RowIndex].FindControl("txtstate") as TextBox; TextBox txtzipcode = gdvProfiles.Rows[e.RowIndex].FindControl("txtzipcode") as TextBox; TextBox txtcountry = gdvProfiles.Rows[e.RowIndex].FindControl("txtcountry") as TextBox; BORegistration oBORegistration = new BORegistration(); BalRegistration oBalRegistration = new BalRegistration(); oBORegistration.Name = lblusername.Text; oBORegistration.Name = txtname.Text; oBORegistration.Sponser = txtsponser.Text; oBORegistration.Email = txtemail.Text; oBORegistration.Dob = txtdob.Text; oBORegistration.HomeContact = txthomecontact.Text; oBORegistration.Contact = txtcontact.Text; oBORegistration.Addressx = txtaddress.Text; oBORegistration.City = txtcity.Text; oBORegistration.State = txtstate.Text; oBORegistration.ZipCode = txtzipcode.Text; oBORegistration.Country = txtcountry.Text; if (oBalRegistration.Updatememberprofile(oBORegistration)) { gdvProfiles.EditIndex = -1; // FillData(); gdvProfiles.DataBind(); } } catch { throw; } }
the SqlDataSounce is
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Universal %>" ProviderName="System.Data.SqlClient" SelectCommand="spGetMemberProfile" SelectCommandType="StoredProcedure" FilterExpression="Username LIKE '{0}%' OR Name LIKE '{0}%' OR Sponser LIKE '{0}%' OR Email LIKE '{0}%' OR Dob LIKE '{0}%' OR HomeContact LIKE '{0}%' OR Contact LIKE '{0}%' OR City LIKE '{0}%' OR Statex LIKE '{0}%' OR ZipCode LIKE '{0}%' OR Country LIKE '{0}%' "> <FilterParameters> <asp:ControlParameter Name="ID" ControlID="txtFilter" /> </FilterParameters> </asp:SqlDataSource>
解决方案I have done this by mention UpdateCommand in SqlDataSqouce as
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Universal %>" ProviderName="System.Data.SqlClient" SelectCommand="spGetMemberProfile" UpdateCommand="spGetMemberProfile" SelectCommandType="StoredProcedure" FilterExpression="Username LIKE '{0}%' OR Name LIKE '{0}%' OR Sponser LIKE '{0}%' OR Email LIKE '{0}%' OR Dob LIKE '{0}%' OR HomeContact LIKE '{0}%' OR Contact LIKE '{0}%' OR City LIKE '{0}%' OR Statex LIKE '{0}%' OR ZipCode LIKE '{0}%' OR Country LIKE '{0}%' "> <FilterParameters> <asp:ControlParameter Name="ID" ControlID="txtFilter" /> </FilterParameters> </asp:SqlDataSource>
这篇关于更新在asp.net中的GridView时的SqlDataSource绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文