how to move focus out of text box in c# asp.net? - c#

I have search box that take username and search on it in the userlist
the problem is that I want from the program to search directly as he get first letter from the user
Front-End
<asp:TextBox ID="TextBox1"
class="form-control table-search-input"
ontextchanged="TextBox1_TextChanged"
AutoPostBack="True"
runat="server" >
Back-End
protected void TextBox1_TextChanged(object sender, EventArgs e )
{
int bb=TextBox1.Text.Length;
adminuserlist2 = adminuserlist2.Where(o => o.AdminUserName.Substring(0,bb) == TextBox1.Text).ToList();
if (adminuserlist2 != null)
{
Table1.Rows.Clear();
Table1.BorderWidth = 1;
Table1.BorderStyle = BorderStyle.Groove;
Table1.GridLines = GridLines.Both;
TableCell un = new TableCell();
un.Text = "UserName";
TableCell pass = new TableCell();
pass.Text = "Password";
TableCell email = new TableCell();
email.Text = "Email";
TableCell isactives = new TableCell();
isactives.Text = "IsActive";
TableCell typename = new TableCell();
typename.Text = "Type Name";
TableCell typeid = new TableCell();
typeid.Text = "Type Id";
row = new TableRow();
row.Width = new Unit("120%");
row.BackColor = System.Drawing.Color.White;
row.ControlStyle.Font.Size = 25;
row.ControlStyle.Font.Bold = true;
row.ControlStyle.ForeColor = System.Drawing.Color.Black;
row.Controls.Add(un);
row.Controls.Add(pass);
row.Controls.Add(email);
row.Controls.Add(isactives);
row.Controls.Add(typename);
row.Controls.Add(typeid);
Table1.Controls.Add(row);
row.ControlStyle.Width = 300;
row.ControlStyle.Height = 30;
Table1.Width = 500;
Table1.ControlStyle.Width = 1000;
Table1.Height = 500;
Table1.CellSpacing = 50;
Table1.CellPadding = 50;
for (int i = 0; i < adminuserlist2.Count; i++)
{
TableCell un2 = new TableCell();
un2.Text = adminuserlist2[i].AdminUserName;
TableCell pass2 = new TableCell();
pass2.Text = adminuserlist2[i].AdminUserPassword;
TableCell email2 = new TableCell();
email2.Text = adminuserlist2[i].AdminUserEmail;
TableCell isactives2 = new TableCell();
isactives2.Text = adminuserlist2[i].IsActive.ToString();
TableCell typename2 = new TableCell();
typename2.Text = adminuserlist2[i].AdminUserType;
TableCell typeid2 = new TableCell();
typeid2.Text = adminuserlist2[i].AdminUserTypeID.ToString();
row = new TableRow();
row.Controls.Add(un2);
row.Controls.Add(pass2);
row.Controls.Add(email2);
row.Controls.Add(isactives2);
row.Controls.Add(typename2);
row.Controls.Add(typeid2);
Table1.Controls.Add(row);
}
}
}
I faced a problem that I should click out of the text box to search
please any one can help

It will be better if you go for OnKeyPress Event instead of TextChanged just change the event and it will be working
Front End
<asp:TextBox ID="TextBox1"
class="form-control table-search-input"
onkeypress="__doPostBack(this.name,'OnKeyPress');"
AutoPostBack="True"
runat="server" >
BackEnd
private void TextBox1_OnKeyPress(string ctrlName, string args){
//your code goes here
}

In Asp.Net there is no real textchanged event, it will only fire if the textbox lost focus and if you have set autopostback to true.
What you can do is the Following:
Add A ScriptManager to your aspx page:
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"/>
Add an onkeypress clientside event to your textbox:
<asp:TextBox ID="TextBox1"
class="form-control table-search-input"
ontextchanged="TextBox1_TextChanged"
AutoPostBack="True"
runat="server" onkeypress="CallMyPageMethod();" >
Call your PageMethod:
<script language="javascript" type="text/javascript">
function CallMyPageMethod() {
PageMethods.MyPageMethod();
}
Add a PageMethod to your code behind:
[System.Web.Services.WebMethod]
public static void MyPageMethod()
{
//add your textchanged event code here
}

Related

TextChanged event fires but does not run in code behind

In my aspx file I have the following code:
<asp:PlaceHolder ID="phdHolding" runat="server">
<asp:Label ID="lblWarning" runat="server" Text="" Visible="false" />
<asp:PlaceHolder ID="phdInput" runat="server" Visible="false">
<h3><asp:Label ID="lblArea" runat="server" Text="Area" /></h3>
</asp:PlaceHolder>
</asp:PlaceHolder>
In code behind (c#) I create a table which is added to phdInput. One part of that code is the creation and population of a TextBox (and Label) within a Table Cell. The TextBox is required to call a function on TextChanged.
decimal amount = Convert.ToDecimal(dc.DataScalar("SELECT Item" + rownr + " FROM Companies WHERE CompanyName='" + coy + "' AND BusinessUnit='" + bu + "'"));
TextBox tb = new TextBox(); tb.ID = "txtAmount" + rownr; tb.Width = 70; tb.Text = amount.ToString("N0");
tb.AutoPostBack = true; tb.TextChanged += new EventHandler(UpdateAmounts);
Label lbl = new Label(); lbl.Text= " " + trow["Unit"].ToString();
TableCell tdamount = new TableCell(); tdamount.Controls.Add(tb); tdamount.Controls.Add(lbl);
tr2.Cells.Add(tdamount);
This is all working fine and everything is displayed on the page. However, when the text is changed and you click outside the textbox or press'Enter' etc. the function is not being reached (have checked in VS) and phdInput becomes blank.
I have used this function, and the TextChanged method of calling it, before with no issues so why is it not working now?
I have managed to recreate one problem
phdInput becomes blank
This was suspicious, if it disappears there must be PostBack in the action. Suspicion rises especially because phdInput attribute Visible is set to false on the form.
I suppose form's initialization is happening inside the Page_Load method and one of the code behind lines is to set phdInput attribute Visible to true. However, phdInput disappears after PostBack is fired, so there's probably check
if (!IsPostBack)
Initialization is happening inside of it, which is correct, no need to initialize it every time page reloads. However
phdInput.Visible = true;
should be outside of check if (!IsPostBack) in order to phdInput be always visible.
Next, something fires the PostBack.
However, when the text is changed and you click outside the textbox or
press'Enter' etc. the function is not being reached (have checked in
VS) and phdInput becomes blank.
This sounds like TextChanged event works, it only calls some other event handler.
I hope it will help you find the solution. Here follows the code that recreates the issue phdInput becomes blank (it is poorly written).
.aspx - content of body tag
<form id="form1" runat="server">
<div>
<p>start</p>
<asp:PlaceHolder ID="phdHolding" runat="server">
<asp:Label ID="lblWarning" runat="server" Text="" Visible="false" />
<asp:PlaceHolder ID="phdInput" runat="server" Visible="false" >
<h3><asp:Label ID="lblArea" runat="server" Text="Area" /></h3>
</asp:PlaceHolder>
</asp:PlaceHolder>
<p>end</p>
</div>
</form>
code behind
using System;
using System.Web.UI.WebControls;
namespace WebApplication8
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!IsPostBack)
{
// row 1
int rownr = Convert.ToInt32(1);
TableRow tr = new TableRow();
tr.VerticalAlign = VerticalAlign.Top;
TableCell tdtype = new TableCell();
tdtype.Text = "table cell 1 text";
tdtype.Width = 250;
tdtype.HorizontalAlign = HorizontalAlign.Left;
tr.Cells.Add(tdtype);
decimal amount = Convert.ToDecimal(1);
TextBox tb = new TextBox(); tb.ID = "txtAmount" + rownr; tb.Width = 70; tb.Text = amount.ToString("N0");
tb.AutoPostBack = true; tb.TextChanged += new EventHandler(UpdateAmounts);
Label lbl = new Label(); lbl.Text = " " + "1";
TableCell tdamount = new TableCell(); tdamount.Controls.Add(tb); tdamount.Controls.Add(lbl);
tr.Cells.Add(tdamount);
// row 2
int rownr2 = Convert.ToInt32(2);
TableRow tr2 = new TableRow();
tr2.VerticalAlign = VerticalAlign.Top;
TableCell tdtype2 = new TableCell();
tdtype2.Text = "table cell 2 text";
tdtype2.Width = 250;
tdtype2.HorizontalAlign = HorizontalAlign.Left;
tr2.Cells.Add(tdtype2);
decimal amount2 = Convert.ToDecimal(2);
TextBox tb2 = new TextBox(); tb2.ID = "txtAmount" + rownr2; tb2.Width = 70; tb2.Text = amount2.ToString("N0");
tb2.AutoPostBack = true; tb2.TextChanged += new EventHandler(UpdateAmounts);
Label lbl2 = new Label(); lbl2.Text = " " + "2";
TableCell tdamount2 = new TableCell(); tdamount2.Controls.Add(tb2); tdamount2.Controls.Add(lbl2);
tr2.Cells.Add(tdamount2);
// table
var table = new Table();
table.Rows.Add(tr);
table.Rows.Add(tr2);
phdInput.Controls.Add(table);
lblWarning.Text = "some warning";
lblArea.Text = "some area text";
lblWarning.Visible = true;
phdInput.Visible = true;
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
protected void UpdateAmounts(object sender, EventArgs e)
{
}
}
}
when page is opened
after textbox's text is changed - either one

Dynamically Generated DropDownList web controls hides on selected

I am facing troubles in creation of dynamic web controls. I have a SQL Databounded DropDownlist with DataTextField ="All_Columns" and DataValueField="DATA_TYPE".
Upon DropDownList1_SelectedIndexChanged i must check for the datatype as decimal or Varchar for the Selected value. If it is decimal i must call Createdynamicwebcontrols_decimal() and create DropDownList2. If it is varchar i must call Createdynamicwebcontrols_varchar().
Current Issue: Upon DropDownList2_SelectedIndexChanged i must create two dynamic textboxes and input values must be retained and search and display the data in a JQGrid with the help of Button Click event. But the DDL control hides completely
How to achieve above all features. Help needed please. New to dynamic web controls.
Aspx Code:
<asp:DropDownList ID="DropDownList5" runat="server" DataSourceID="column_list_for_filter" DataTextField="All_Columns" DataValueField="DATA_TYPE" OnSelectedIndexChanged ="DropDownList5_SelectedIndexChanged" AutoPostBack="true" >
</asp:DropDownList>
<asp:SqlDataSource ID="column_list_for_filter" runat="server" ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString %>" SelectCommand="SELECT COLUMN_NAME AS 'All_Columns', DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE (TABLE_NAME = 'TABLE')"></asp:SqlDataSource>
C# code:
protected void Page_Load(object sender, EventArgs e)
{
Panel6.Visible = false;
JQGrid9.Visible = false;
if (Session["DataforSearch"] != null)
{
Panel6.Visible = true;
JQGrid9.Visible = true;
JQGrid9.DataSource = Session["DataforSearch"] as string;
}
}
protected void Page_PreInit(object sender, EventArgs e)
{
//How to Proceed
}
protected void DropDownList5_SelectedIndexChanged(object sender, EventArgs e)
{
if(DropDownList5.SelectedValue == "decimal")
{
createdynamiccontrols_decimal();
}
else if(DropDownList5.SelectedValue == "int")
{
createdynamiccontrols_int();
}
else if(DropDownList5.SelectedValue == "varchar")
{
createdynamiccontrols_varchar();
}
//How to Proceed
}
protected void createdynamiccontrols_decimal()
{
int i = DropDownList5.SelectedIndex;
++i;
TableRow row;
row = new TableRow();
TableCell cell1;
cell1 = new TableCell();
//DropDownList2
DropDownList Range_DDL;
Range_DDL = new DropDownList();
Range_DDL.ID = "RandeDDL" + i.ToString();
Range_DDL.Items.Insert(0, new ListItem("--Select--", "--Select--"));
Range_DDL.Items.Insert(1, new ListItem("Equal", "Equal"));
Range_DDL.Items.Insert(2, new ListItem("NotEqual", "NotEqual"));
Range_DDL.Items.Insert(3, new ListItem("greater than", "greater than"));
Range_DDL.Items.Insert(2, new ListItem("lesser than", "lesser than"));
Range_DDL.Items.Insert(2, new ListItem("greater than or equal to", "greater than or equal to"));
Range_DDL.Items.Insert(2, new ListItem("lesser than or equal to", "lesser than or equal to"));
Range_DDL.Items.Insert(2, new ListItem("Contains", "Contains"));
Range_DDL.Items.Insert(2, new ListItem("Is Null", "Is Null"));
Range_DDL.Items.Insert(2, new ListItem("Is Not Null", "Is Not Null"));
Range_DDL.Items.Insert(2, new ListItem("Between", "Between"));
Range_DDL.AutoPostBack = true;
Range_DDL.SelectedIndexChanged += new System.EventHandler(Range_DDL_SelectedIndexChanged);
cell1.Controls.Add(Range_DDL);
//// Add the TableCell to the TableRow
row.Cells.Add(cell1);
dynamic_filter_table.Rows.Add(row);
dynamic_filter_table.EnableViewState = true;
ViewState["dynamic_filter_table"] = true;
}
//DropdownList2 to create dynamic text boxes
protected void Range_DDL_SelectedIndexChanged(object sender, EventArgs e)
{
int j = DropDownList5.SelectedIndex;
++j;
TableRow row;
row = new TableRow();
TableRow rowtwo;
rowtwo = new TableRow();
TableCell cell1;
cell1 = new TableCell();
TableCell cell2;
cell2 = new TableCell();
TableCell cell3;
cell3 = new TableCell();
TextBox tb1;
tb1 = new TextBox();
TextBox tb2;
tb2 = new TextBox();
Label lbl1;
lbl1 = new Label();
Label lbl2;
lbl2 = new Label();
//// Set a unique ID for each TextBox added
tb1.ID = "lowerbound_" + j.ToString();
tb2.ID = "upperbound_" + j.ToString();
lbl1.Text = "LowerBound:";
lbl1.Font.Size = FontUnit.Point(10);
lbl1.Font.Bold = true;
lbl1.Font.Name = "Arial";
lbl2.Text = "UpperBound:";
lbl2.Font.Size = FontUnit.Point(10);
lbl2.Font.Bold = true;
lbl2.Font.Name = "Arial";
Button addmore;
addmore = new Button();
addmore.ID = "Button" + j.ToString();
addmore.Text = "+";
addmore.Click += new System.EventHandler(addmore_click);
cell1.Controls.Add(lbl1);
cell2.Controls.Add(tb1);
cell2.Controls.Add(lbl2);
cell2.Controls.Add(tb2);
cell3.Controls.Add(addmore);
row.Cells.Add(cell1);
row.Cells.Add(cell2);
rowtwo.Cells.Add(cell3);
dynamic_filter_table.Rows.Add(row);
dynamic_filter_table.EnableViewState = true;
ViewState["dynamic_filter_table"] = true;
}
//Button Click to retrieve the input from dynamic generated text box and display in JQGrid.
protected void Button1_Click(object sender, EventArgs e)
{
int j = DropDownList5.SelectedIndex;
++j;
Panel6.Visible = true;
JQGrid9.Visible = true;
TextBox lowerboundd = dynamic_filter_table.FindControl("lowerbound_" + j.ToString()) as TextBox;
TextBox upperbound = dynamic_filter_table.FindControl("upperbound_" + j.ToString()) as TextBox;
con.Open();
SqlDataAdapter da = new SqlDataAdapter("SELECT a,b,c FROM TABLE WHERE " + DropDownList5.SelectedValue + " >= " + lowerboundd.Text + " AND " + DropDownList5.SelectedValue + " <= " + upperbound.Text, con);
**//Problems in retrieving the input of textboxes - Object Reference error**
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
Session["DataforSearch"] = ds.Tables[0];
}
You can find the source code and example on following link :
http://www.codeproject.com/Articles/20714/Dynamic-ASP-NET-control-creation-using-C
I also found this code online, I hope it would help you: You can modify this according to your needs in this code Button is added on the click of an another button.
Adding a new control to a form is really easy.
All you have to do is create a new instance of the control like any other class.
Eg:
Button NewButton = new button();
Then you fill out the properties of the control.
NewButton.Text = "My New Button";
After this setup an event handler.
NewButton.Click += new EventHandler(NewButton_Click);
Then in the button handler to tell which dynamic button was pressed you use the sender parameter.
void NewButton_Click(object sender, EventArgs e)
{
Button CurrentButton = (Button)sender;
CurrentButton.Text = "I was clicked";
}
Then finally the last step in setting the button up is to add the control to the form.
this.Controls.Add(NewButton);

unable to add more than one server control dynamically in asp.net

Hi I am really stuck up in one of my development tasks since couple of days and I am unable to find out the exactly whats going on. Please help.
I am trying to add rows dynamically to the webpage as follows.
I want to use server control. But I am unable to add more than one row.
No luck even though I used session variable. Kindly help please :)
-----------------aspx file---------------
<div>
<asp:Table ID="tbl" runat="server">
<asp:TableRow ID="rw0">
<asp:TableCell ID="c01" Width="100px">
<asp:CheckBox runat="server" ID="chk0" />
</asp:TableCell>
<asp:TableCell ID="c02" Width="100px">
<asp:TextBox runat="server" ID="txt0" />
</asp:TableCell></asp:TableRow>
<asp:TableRow ID="rw1">
<asp:TableCell ID="c11" Width="100px">
<asp:CheckBox ID="chk1" runat="server" />
</asp:TableCell><asp:TableCell ID="c12" Width="100px">
<asp:TextBox runat="server" ID="txt1" />
</asp:TableCell></asp:TableRow>
</asp:Table>
<asp:Button ID="btn1" runat="server" Text="Add Row" OnClick="addRow" />
</div>
---------------------C# code behind------------------------------
protected void addRow(object sender, EventArgs e)
{
int num_row = new int(); //checkpoint
num_row = (tbl.Rows).Count;
if (Session["tables"] != null)
{
tbl = (Table)Session["tables"];
}
TableRow row = new TableRow();
TableCell cell1 = new TableCell();
TableCell cell2 = new TableCell();
TextBox tb = new TextBox();
CheckBox cb = new CheckBox();
row.ID = "rw" + num_row;
cell1.ID = "c" + num_row + "1";
cell2.ID = "c" + num_row + "2";
tb.ID = "txt" + num_row;
tb.EnableViewState = true;
cb.ID = "chk" + num_row;
cell1.Controls.Add(tb);
cell2.Controls.Add(cb);
row.Cells.Add(cell1);
row.Cells.Add(cell2);
tbl.Rows.Add(row);
Session["tables"] = tbl;
}
You are able to see only one row added everytime because, dynamically created controls will not be available across postbacks.
When you click on add row first time, a post back happens, a third row is added. when you click in add row second time, the already added row would not be available and a new row gets added again. Finally you will be able to see only one row everytime.
Bottom line is you have to recreate the dynamically added controls everytime on postback in the page_load event, the reason being dynamically added server sontrols donot persist across postbacks
refer Why dynamically created user controls disappear when controls are not doing full postbacks? also
To maintain the viewstate of dynamically added controls you have to generate ids for the controls. When you recreate the controls during postback, recreate them with the same ids so that view state is maintained.
Use some standard logic to generate the ids for the controls. hope this helps.
Update:
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
int num_row = (int)Session["No_of_Rows"];
for (int i = 2; i < num_row; i++)
{
TableRow row = new TableRow();
TableCell cell1 = new TableCell();
TableCell cell2 = new TableCell();
TextBox tb = new TextBox();
CheckBox cb = new CheckBox();
row.ID = "rw" + i;
cell1.ID = "c" + i + "1";
cell2.ID = "c" + i + "2";
tb.ID = "txt" + i;
tb.EnableViewState = true;
cb.ID = "chk" + i;
cell1.Controls.Add(cb);
cell2.Controls.Add(tb);
row.Cells.Add(cell1);
row.Cells.Add(cell2);
tbl.Rows.Add(row);
}
}
else
{
Session["No_of_Rows"] = 2;
}
}
protected void addRow(object sender, EventArgs e)
{
int num_row = (int)Session["No_of_Rows"]+1;
TableRow row = new TableRow();
TableCell cell1 = new TableCell();
TableCell cell2 = new TableCell();
TextBox tb = new TextBox();
CheckBox cb = new CheckBox();
row.ID = "rw" + num_row;
cell1.ID = "c" + num_row + "1";
cell2.ID = "c" + num_row + "2";
tb.ID = "txt" + num_row;
tb.EnableViewState = true;
cb.ID = "chk" + num_row;
cell1.Controls.Add(cb);
cell2.Controls.Add(tb);
row.Cells.Add(cell1);
row.Cells.Add(cell2);
tbl.Rows.Add(row);
Session["No_of_Rows"] = tbl.Rows.Count;
}
After entering the value in the tale column how to save all the values in the database table?

button event function won't fire (postback issue)

I have a aspx page which gets updated dynamically on button click. a table with buttons is being added to a placeholder. each button should redirect to another page sending it's id in a query string.
the problem I'm having is that when each button is clicked a postback occurs and the buttons are not added into the page again (because the method is being run by button click), and so the function fired by the event isn't running
I tried to put everything inside an updatepanel except the placeholder, but then the table is not added in the first place. how can I call the btn_click function again from the pageload? what should I pass in its parameters?
or maybe I can add buttons that won't casue postback?
this is the aspx:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<h1 style="text-align:center;">חיפוש חברים</h1>
<br />
<table>
<tr><td>אזור עבודה מועדף</td>
<td>סיווג משני</td>
<td>סיווג ראשי</td>
<td>שם משפחה</td>
<td>שם</td>
<td>תז</td>
</tr>
<tr>
<td>
<asp:DropDownList ID="working_area" CssClass="wid" runat="server">
<%--some items--%>
</asp:DropDownList>
</td>
<td>
<asp:DropDownList ID="d_d_second" CssClass="wid" runat="server">
</asp:DropDownList>
</td>
<td>
<asp:DropDownList ID="d_d_main" CssClass="wid" runat="server">
<asp:ListItem></asp:ListItem>
</asp:DropDownList>
</td>
<td><asp:TextBox ID="m_l_name" CssClass="wid" runat="server"></asp:TextBox></td>
<td><asp:TextBox ID="m_name" CssClass="wid" runat="server"></asp:TextBox></td>
<td><asp:TextBox ID="m_id" CssClass="wid" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style="text-align:left"><asp:Button ID="search" CssClass="btn btn-primary"
runat="server" Text="חפש" onclick="search_Click" /></td>
</tr>
</table>
<asp:PlaceHolder ID="search_tbl_ph" runat="server"></asp:PlaceHolder>
</ContentTemplate>
</asp:UpdatePanel>
this is the code behind:
protected void Page_Load(object sender, EventArgs e)
{
}
protected void search_Click(object sender, EventArgs e)
{
search_category s1 = new search_category();
s1.id = m_id.Text;
s1.name = m_name.Text;
s1.l_name = m_l_name.Text;
s1.main_cat = d_d_main.SelectedValue;
s1.second_cat = d_d_second.SelectedValue;
s1.working_area = working_area.SelectedValue;
List<member> m_list = db.return_search_member(s1);
Table m_tbl = new Table();
TableRow r2 = new TableRow();
TableCell c7 = new TableCell();
TableCell c8 = new TableCell();
TableCell c9 = new TableCell();
TableCell c10 = new TableCell();
TableCell c11 = new TableCell();
TableCell c12 = new TableCell();
c7.Text = "תז";
c8.Text="שם פרטי";
c9.Text="שם משפחה";
c10.Text="סיווג ראשי";
c11.Text="סיווג משני";
r2.Controls.Add(c12);
r2.Controls.Add(c11);
r2.Controls.Add(c10);
r2.Controls.Add(c9);
r2.Controls.Add(c8);
r2.Controls.Add(c7);
r2.CssClass = " head_line";
m_tbl.Controls.Add(r2);
foreach (member m1 in m_list)
{
TableRow r1 = new TableRow();
TableCell c1 = new TableCell();
TableCell c2 = new TableCell();
TableCell c3 = new TableCell();
TableCell c4 = new TableCell();
TableCell c5 = new TableCell();
TableCell c6 = new TableCell();
Button btn1 = new Button { Text = "עבור לכרטיס חבר", CommandArgument = "argument", ID = m1.id };
btn1.Click += new EventHandler(btn_click);
btn1.CssClass = "btn btn-primary";
c1.Controls.Add(btn1);
c2.Text = m1.prof.secondary;
c3.Text = m1.prof.primary;
c4.Text = m1.l_name;
c5.Text = m1.f_name;
c6.Text = m1.id;
r1.Controls.Add(c1);
r1.Controls.Add(c2);
r1.Controls.Add(c3);
r1.Controls.Add(c4);
r1.Controls.Add(c5);
r1.Controls.Add(c6);
m_tbl.Controls.Add(r1);
}
search_tbl_ph.Controls.Add(m_tbl);
}
protected void btn_click(object sender, EventArgs e)
{
Button btn = (Button)sender;
String member_id = btn.ID;
string qstring = "?id=" + member_id;
Response.Redirect("member_page.aspx" + qstring);
//Session["id"] = qstring;
}
private void MakeButton()
{
search_category s1 = new search_category();
s1.id = m_id.Text;
s1.name = m_name.Text;
s1.l_name = m_l_name.Text;
s1.main_cat = d_d_main.SelectedValue;
s1.second_cat = d_d_second.SelectedValue;
s1.working_area = working_area.SelectedValue;
List<member> m_list = db.return_search_member(s1);
Table m_tbl = new Table();
TableRow r2 = new TableRow();
TableCell c7 = new TableCell();
TableCell c8 = new TableCell();
TableCell c9 = new TableCell();
TableCell c10 = new TableCell();
TableCell c11 = new TableCell();
TableCell c12 = new TableCell();
c7.Text = "תז";
c8.Text="שם פרטי";
c9.Text="שם משפחה";
c10.Text="סיווג ראשי";
c11.Text="סיווג משני";
r2.Controls.Add(c12);
r2.Controls.Add(c11);
r2.Controls.Add(c10);
r2.Controls.Add(c9);
r2.Controls.Add(c8);
r2.Controls.Add(c7);
r2.CssClass = " head_line";
m_tbl.Controls.Add(r2);
foreach (member m1 in m_list)
{
TableRow r1 = new TableRow();
TableCell c1 = new TableCell();
TableCell c2 = new TableCell();
TableCell c3 = new TableCell();
TableCell c4 = new TableCell();
TableCell c5 = new TableCell();
TableCell c6 = new TableCell();
Button btn1 = new Button { Text = "עבור לכרטיס חבר", CommandArgument = "argument", ID = m1.id };
btn1.Click += new EventHandler(btn_click);
btn1.CssClass = "btn btn-primary";
c1.Controls.Add(btn1);
c2.Text = m1.prof.secondary;
c3.Text = m1.prof.primary;
c4.Text = m1.l_name;
c5.Text = m1.f_name;
c6.Text = m1.id;
r1.Controls.Add(c1);
r1.Controls.Add(c2);
r1.Controls.Add(c3);
r1.Controls.Add(c4);
r1.Controls.Add(c5);
r1.Controls.Add(c6);
m_tbl.Controls.Add(r1);
}
search_tbl_ph.Controls.Add(m_tbl);
}
protected void Page_Load(object sender, EventArgs e)
{
if(ViewState["ClickEventFired"]!=null && ViewState["ClickEventFired"]==true)
{
MakeButton();
}
}
protected void search_Click(object sender, EventArgs e)
{
MakeButton();
ViewState["ClickEventFired"]=true;
}
protected void btn_click(object sender, EventArgs e)
{
// your code
}
You have to reassign click event after Postback.
let me know if you help this.
Maybe you could use a simple <a href=""> link instead of a button ?
Otherwise, you can use the OnClientClick attribute of the button, not the OnClick event because it will cause a postback.
Its recommended to load the dynamic controls during the Page_Init instead, because we may want to hook up our events with proper handler at an early stage.
protected void Page_Init(object sender, EventArgs e)
{
///code to create dynamic controls
}
Button OnClick Event Only gets fired if the button is inside a Form element.
Just put your HTML inside a Form.
I tried it with your code and was able to see search_Click gets called when user click on the button.

How to add Dynamic Field in Dynamic Page in ASP.NET in C#

I want to add one Dynamic Field as "ADD MORE SKILLS" which shows some Textbox and Label will come on clicking this link. You can see this kind of example on some Shine.com, TimesJob etc.....
Here is Something to start with. Modify According to your need. Create a new button and onclick of button create the new controls dynamically. I have created a label and textbox dynamically in the below mentioned code
ASP Button
<asp:Button ID="AddMoreSkills" runat="server" Text="Add More Skills"
onclick="AddMoreSkills_Click" />
OnClick event in C#
protected void AddMoreSkills_Click(object sender, EventArgs e)
{
Table tblmain = new Table();
tblmain.ID = "tblmain";
tblmain.Width = Unit.Percentage(100);
tblmain.Attributes.CssStyle.Add("margin-top", "5px");
tblmain.Attributes.CssStyle.Add("margin-bottom", "5px");
TableCell tblTCell;
TableRow tblRow = new TableRow();
TableCell tblCell = new TableCell();
tblRow = new TableRow();
//Create Label Dynamically
tblCell = new TableCell();
Label lblTown = new Label();
lblTown.ID = "lblSkill";
lblTown.Text = "Skill";
//Add label to table cell
tblCell.Controls.Add(lblTown);
tblRow.Cells.Add(tblCell);
//Create TextBox Dynamically
TextBox txtSkill = new TextBox();
txtSkill.ID = "txtSkill";
//Add TextBox to table cell
tblTCell = new TableCell();
tblTCell.Controls.Add(txtSkill);
tblRow.Cells.Add(tblTCell);
tblmain.Rows.Add(tblRow);
form1.Controls.Add(tblmain);
}

Categories

Resources