|
-
Mar 26th, 2007, 11:24 PM
#1
Thread Starter
Addicted Member
[2005] Button click event is not fired. why?
I am inheriting Gridview to add a functionality to add a record in the gridview. I bind the gridview with the sqldatasource. Assume that the category table has two fields "code" & "name"
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
namespace GridUpControl
{
public class GridCustomControl : GridView, INamingContainer
{
public Button btn = new Button();
public TextBox txtbx = new TextBox();
public void ShowAddButton()
{
btn.Text = "Add";
btn.Click += new EventHandler(btn_Click);
//Controls.Add(btn);
}
public void btn_Click(object sender,EventArgs e)
{
SqlConnection conn = new SqlConnection(ConnectionString);
SqlCommand cmd = new SqlCommand(InsertText, conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
this.DataBind();
}
public GridCustomControl() : base()
{
}
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string InsertText
{
get
{
//EnsureChildControls();
String s = (String)ViewState["InsertText"];
return ((s == null) ? String.Empty : s);
}
set
{
//EnsureChildControls();
ViewState["InsertText"] = value;
}
}
public string ConnectionString
{
get
{
//EnsureChildControls();
String s = (String)ViewState["ConnectionString"];
return ((s == null) ? String.Empty : s);
}
set
{
// EnsureChildControls();
ViewState["ConnectionString"] = value;
}
}
protected override void Render(HtmlTextWriter writer)
{
base.Render(writer);
btn.RenderControl(writer);
}
}
}
Utilizing Code in default.aspx:
Code:
protected void Page_Load(object sender, EventArgs e)
{
GridCustomControl1.ShowFooter = true;
GridCustomControl1.ConnectionString = ConfigurationManager.ConnectionStrings["TraineeDBConnectionString"].ConnectionString;
TextBox CatName = (TextBox)GridCustomControl1.FooterRow.FindControl("CategoryNameTextBox");
GridCustomControl1.InsertText = "insert into ec_category(name) values('" + CatName.Text + "')";
GridCustomControl1.ShowAddButton();
}
Now, the button click event is not fired. It remains ideal. Please help. I don't know the above code is optimum or not. Please help.
God has been pleased to place as a king or cobbler do the work sincerely
-
Mar 27th, 2007, 11:26 AM
#2
Re: [2005] Button click event is not fired. why?
Because you're essentially doing the same thing as you did in your previous post. You're rendering the control to the output stream and expecting it to behave as an ASP.NET rendered control.
You want a single Add button next to your Grid, so you should create a web user control. I assume you know what that is. Place your GridCustomControl (without the Add button) on a web user control page, and under it, an Add button. You can then handle the Add button's click event in the web user control.
-
Mar 27th, 2007, 10:59 PM
#3
Thread Starter
Addicted Member
Re: [2005] Button click event is not fired. why?
If I do like you told, the design time properties (setting datasources, smart tag properties) of the gridview can't be set in the design mode. Or, anyother alternative ways to do the think?. Mendhak help me.
God has been pleased to place as a king or cobbler do the work sincerely
-
Mar 28th, 2007, 10:36 AM
#4
Re: [2005] Button click event is not fired. why?
But the method I suggested allows for more design time control. The gridview will be visible at design time. So will the button. That's what you want isn't it? If not, explain further.
-
Mar 29th, 2007, 01:26 AM
#5
Thread Starter
Addicted Member
Re: [2005] Button click event is not fired. why?
I wish to inherit all the properties and procedures of the gridview. The gridview should have column type as some templatefield that should be set through html code as in normal gridivew template style.
God has been pleased to place as a king or cobbler do the work sincerely
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|