merlin_vista
یک شنبه 29 مهر 1386, 16:43 عصر
آقا من تازه از روش Wizard به کد نویسی مهاجرت کردم و حالا نمیدانم که چگونه برای GridView دکمه Edit بنویسم : من از این روش استفاده کردم ولی خوشم نیامد چون تتمام چیز ها را در ASP باید بنویسم و میخواهم کد ها را در C# بنویستم
ان هم روش من :
<asp:SqlDataSource ID="linkdo" runat="server" ProviderName="System.Data.SqlClient" ConnectionString="Data Source=(local);Initial Catalog=asannet;Persist Security Info=True;User ID=asannet;Password=5236651" SelectCommand="select * from LinkDostan" SelectCommandType="Text" UpdateCommand="UPDATE LinkDostan SET Name = @Name,link= @link, toltip = @toltip WHERE id = @id" UpdateCommandType="Text" DeleteCommand="DELETE FROM LinkDostan WHERE id = @id" DeleteCommandType="Text" >
<UpdateParameters>
<asp:Parameter Type="String" Name="Name" />
<asp:Parameter Name="link" Type="String" />
<asp:Parameter Type="String" Name="toltip" />
</UpdateParameters>
<DeleteParameters>
<asp:Parameter Type="String" Name="Name" />
<asp:Parameter Name="link" Type="String" />
<asp:Parameter Type="String" Name="toltip" />
</DeleteParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" DataSourceID="linkdo" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="id" CssClass="fon">
<Columns>
<asp:TemplateField HeaderText="لینک دوستان">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" CssClass="fon" NavigateUrl='<%# DataBinder.Eval(Container.DataItem, "link") %>' Target="_blank" ToolTip='<%# DataBinder.Eval(Container.DataItem, "toltip") %>'><%# DataBinder.Eval(Container.DataItem, "Name") %></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ButtonType="Image" CancelText="" DeleteImageUrl="~/Image/icon/icon-remove.gif"
DeleteText="حذف" EditText="" InsertText="" NewText="" SelectText="" ShowDeleteButton="True"
ShowHeader="True" UpdateText="" />
</Columns>
</asp:GridView>
و حالا میخواهم از این روش استفاده نکنم لطفاً یک روش خوب و بهینه برای این کار جایگزین کنم و از آن استفاده کنم !!
در ضمن اگر امکان دارد روش ساخت کلید Delet را هم بیان کنید چون همان طور که میبینید در کد بالا من آرگومان را که برابر آی دی سطر میباشد برای دکمه حذف فرسادم ولی این روش فقط در رویداد RowCommand کار داد و من میخواهم در رویداد Delete بنویسم که خطا گرفت اگه میشه راهنمایی کنید !!
در RowCommand اینجوری نوشتم :
public partial class DataGrid : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
SqlCommand cmd = new SqlCommand();
SqlDataAdapter dat = new SqlDataAdapter();
DataSet dst = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void gdvAuthors_RowCommand(object sender, GridViewCommandEventArgs e)
{
cmd.Connection = con;
cmd.CommandText = "Delete From TBinfo where id=@id";
cmd.CommandType = CommandType.Text;
string r = e.CommandArgument.ToString();
cmd.Parameters.AddWithValue("@id", int.Parse(r));
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
در ضمن اگه میشه بگید که چگونه به سطر های دیتاست دسترسی داشته باشم و مثلا از سطر اول ستون نام را در یک متغییر ذخیره کنم !!
ان هم روش من :
<asp:SqlDataSource ID="linkdo" runat="server" ProviderName="System.Data.SqlClient" ConnectionString="Data Source=(local);Initial Catalog=asannet;Persist Security Info=True;User ID=asannet;Password=5236651" SelectCommand="select * from LinkDostan" SelectCommandType="Text" UpdateCommand="UPDATE LinkDostan SET Name = @Name,link= @link, toltip = @toltip WHERE id = @id" UpdateCommandType="Text" DeleteCommand="DELETE FROM LinkDostan WHERE id = @id" DeleteCommandType="Text" >
<UpdateParameters>
<asp:Parameter Type="String" Name="Name" />
<asp:Parameter Name="link" Type="String" />
<asp:Parameter Type="String" Name="toltip" />
</UpdateParameters>
<DeleteParameters>
<asp:Parameter Type="String" Name="Name" />
<asp:Parameter Name="link" Type="String" />
<asp:Parameter Type="String" Name="toltip" />
</DeleteParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" DataSourceID="linkdo" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="id" CssClass="fon">
<Columns>
<asp:TemplateField HeaderText="لینک دوستان">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" CssClass="fon" NavigateUrl='<%# DataBinder.Eval(Container.DataItem, "link") %>' Target="_blank" ToolTip='<%# DataBinder.Eval(Container.DataItem, "toltip") %>'><%# DataBinder.Eval(Container.DataItem, "Name") %></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ButtonType="Image" CancelText="" DeleteImageUrl="~/Image/icon/icon-remove.gif"
DeleteText="حذف" EditText="" InsertText="" NewText="" SelectText="" ShowDeleteButton="True"
ShowHeader="True" UpdateText="" />
</Columns>
</asp:GridView>
و حالا میخواهم از این روش استفاده نکنم لطفاً یک روش خوب و بهینه برای این کار جایگزین کنم و از آن استفاده کنم !!
در ضمن اگر امکان دارد روش ساخت کلید Delet را هم بیان کنید چون همان طور که میبینید در کد بالا من آرگومان را که برابر آی دی سطر میباشد برای دکمه حذف فرسادم ولی این روش فقط در رویداد RowCommand کار داد و من میخواهم در رویداد Delete بنویسم که خطا گرفت اگه میشه راهنمایی کنید !!
در RowCommand اینجوری نوشتم :
public partial class DataGrid : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
SqlCommand cmd = new SqlCommand();
SqlDataAdapter dat = new SqlDataAdapter();
DataSet dst = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void gdvAuthors_RowCommand(object sender, GridViewCommandEventArgs e)
{
cmd.Connection = con;
cmd.CommandText = "Delete From TBinfo where id=@id";
cmd.CommandType = CommandType.Text;
string r = e.CommandArgument.ToString();
cmd.Parameters.AddWithValue("@id", int.Parse(r));
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
در ضمن اگه میشه بگید که چگونه به سطر های دیتاست دسترسی داشته باشم و مثلا از سطر اول ستون نام را در یک متغییر ذخیره کنم !!