ASP.NET Table control - save button postback loses all my rows and cells - asp.net

I have a ASP.NET Table control which I have dragged to my page. I have to create the rows and columns dynamically which I have also done. Some of the cells in my table are textboxes. I want to be able to modify the textbox values and save the changes. I have added a button within the same panel as the table but when I try to loop through the tables rows I get zero.
Why is the table not remembering my rows and cells? As I have already loaded the data once I don't want to rebuild the table each time a button is pressed. I think by creating the table each time in Page_Load I'd be going back and forth to the database server. I really want to avoid this.

You can store your dynamically created table in view that you can fetch after rendering.
SqlConnection con =new SqlConnection(ConfigurationManager.ConnectionStrings["dbstring"].ConnectionString);
private int ctrName = 0;
Table tableName = null;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
tableName = new Table();
tableName.ID = "tableName";
Session["tableName"] = tableName;
ViewState["ctrName"] = ctrName;
}
ctrName = (Int32)ViewState["ctrName"];
tableName = (Table)Session["tableName"];
pnlName.Controls.Add(tableName);
}
private void GenerateName()
{
ctrName++;
TableRow row = new TableRow();
TableCell cell = new TableCell();
TextBox tb = new TextBox();
tb.ID = "Txt_Name" + ctrName;
tb.Width = 150;
cell.Controls.Add(tb);
row.Cells.Add(cell);
tableName.Rows.Add(row);
Session["tableName"] = tableName;
ViewState["ctrName"] = ctrName;
}
For Access the table details:
tableName = (Table)Session["tableName"];

Related

how to bind combobox and web service in asp.net

I have a combo box and a web service. I want to call web service and then when I open my combo I see the values the are in my web service.My code did not work.Can anybody help me?
protected void Page_Load(object sender, EventArgs e)
{
string firstParam = "1";
DataSet ds2 = ws1.Automation_H022_MamooriyatSaati_View(firstParam, secondParam, thirdParam);
this.GridView1.DataSource = ds2;
foreach (System.Data.DataTable table1 in ds1.Tables)
{
foreach (DataRow dr in table1.Rows)
{
cmbox.DataTextField = "Tp_Place";
cmbox.DataValueField = "Lg_Personality";
cmbox.DataBind();
cmbox.Items.Insert(0, new ListItem("", "1"));
}
}
}
You are adding not adding anything to the cmbox in the foreach (DataRow dr in table1.Rows). And even if you did the next loop would overwrite it.
You need to set the datasource for the ComboBox in the same way you set the GridView.
If there is more than one table in ds1 and you want both tables to be visible in the combobox then you need to merge those two (or more) tables first and then bind them as a single source of data.
DataSet ds2 = ws1.Automation_H022_MamooriyatSaati_View(firstParam, secondParam, thirdParam);
this.GridView1.DataSource = ds2;
cmbox.DataSource = ds1.Tables[0];
cmbox.DataTextField = "Tp_Place";
cmbox.DataValueField = "Lg_Personality";
cmbox.DataBind();
cmbox.Items.Insert(0, new ListItem("", "1"));

jqgrid footervalue not correct if rows is empty

I have a jqgrid (ver 4.6.0.0), I show a grant total on footer of a amount column. If user insert a new row or delete a row, it should recalculate the total amount, and show it on footer. It works fine until the grid is empty(no any rows), I mean if user delete the last row on grid, the code still calculate the correct amount ( 0 ), but the footer still show the total amount it was, not 0.
Here is my code:
protected void Jqgrid1_DataRequested(object sender, JQGridDataRequestedEventArgs e)
{
DataSet _ds = (DataSet)Session["myApplicationForm"];
DataTable dt1 = _ds.Tables[1] as DataTable;
CalcVoucherAmt(dt1);
}
private void CalcVoucherAmt(DataTable _dt)
{
decimal amtSum = 0;
foreach (DataRow dr in _dt.Rows)
{
amtSum += obj2decimal(dr["DETAIL_AMT"]);
}
Jqgrid1.Columns.FromDataField("DETAIL_AMT").FooterValue = amtSum.ToString();
}
protected void Jqgrid1_RowAdding(object sender, Trirand.Web.UI.WebControls.JQGridRowAddEventArgs e)
{
DataSet _ds = (DataSet)Session["myApplicationForm"];
DataTable dt1 = _ds.Tables[1] as DataTable;
dt1.PrimaryKey = new DataColumn[] { dt1.Columns["DATA_SEQ"] };
DataRow row = dt1.NewRow();
row["UNIQ_KEY"] = System.Guid.NewGuid().ToString();
row["DETAIL_AMT"] = amt; //user input
dt1.Rows.InsertAt(row, dt1.Rows.Count);
Session["myApplicationForm"] = _ds;
Jqgrid1.DataSource = dt1;
Jqgrid1.DataBind();
}
protected void Jqgrid1_RowEditing(object sender, Trirand.Web.UI.WebControls.JQGridRowEditEventArgs e)
{
DataSet _ds = (DataSet)Session["myApplicationForm"];
DataTable dt1=_ds.Tables[1] as DataTable;
dt1.PrimaryKey = new DataColumn[] { dt1.Columns["DATA_SEQ"] };
DataRow rowEdited = dt1.Rows.Find(e.RowKey);
rowEdited["DETAIL_AMT"] = amt; //user input
Session["myApplicationForm"] = _ds;
Jqgrid1.DataSource = dt1;
Jqgrid1.DataBind();
}
protected void Jqgrid1_RowDeleting(object sender, JQGridRowDeleteEventArgs e)
{
DataSet _ds = (DataSet)Session["myApplicationForm"];
DataTable dt1 = _ds.Tables[1] as DataTable;
dt1.PrimaryKey = new DataColumn[] { dt1.Columns["DATA_SEQ"] };
DataRow rowToDelete = dt1.Rows.Find(e.RowKey);
if (rowToDelete != null)
dt1.Rows.Remove(rowToDelete);
}
and in aspx, the code is
<cc1:JQGrid ID="Jqgrid1" runat="server" Height="100%" Width="100%" AutoWidth="true" PagerSettings-PageSizeOptions="[]"
OnRowAdding="Jqgrid1_RowAdding"
OnRowEditing="Jqgrid1_RowEditing"
OnDataRequested="Jqgrid1_DataRequested"
OnRowDeleting="Jqgrid1_RowDeleting">
</cc1:JQGrid>
any idea?
Why you decide that jqGrid "should recalculate the total amount" automatically? If you insert a row using or delete a row you can specify callback which will be called at the end of Add/Delete operation. Inside of the callback you can use getCol to recalculate the total amount and to use footerData to set new value in the footer.
Alternatively you can trigger reloadGrid which will sort new data correctly, recalculate the total amount and to place it on the footer row. Both ways will work.
The code which you posted seems to have mo relation to jqGrid. It don't show which editing mode and how you use to Add/Delete rows. So I can't give you more detailed implementation tips.

ASP.NET / Sharepoint - Checkbox.CheckChanged event fires same Event of other (same) control from bevore

I'm working on a Webpart for SharePoint with ASP.NET
On every click my page will be reloaded and I add a List of Checkboxes with different ID's in two tables and a event to handle the CheckedChanged Event (all the same).
In Addition the autopostback is set to true.
When I run and click my first checkbox every thinks work fine.
By clicking the next checkbox in the same table, I get in my eventReceiver 2! Events - first, the right one and after that the checkbox was clicked before.
As many boxes I click, as many checkboxes fire their Event (when used before).
Only difference is between the 2 Tables. Here clicking in first table all is ok, clicking in second table all ok... after that each table has the same effect. Click in first table again, I get 2 Events to handle, click in the second one these 2 to handle.
I have no idea what's going wrong.
Here's some code
CheckBox eMailNotifikation = new CheckBox() { TextAlign = TextAlign.Right };
eMailNotifikation.ID = #anCounter + "_" + #anName + "_" + #anothername + "_" + "mail_checkbox";
eMailNotifikation.AutoPostBack = true;
eMailNotifikation.CheckedChanged -= new EventHandler(eMailNotifikation_CheckedChanged);
eMailNotifikation.Checked = #setInitialValue;
//Add Event
eMailNotifikation.CheckedChanged += new EventHandler(eMailNotifikation_CheckedChanged);
cell.Controls.Add(eMailNotifikation);
that's it for creation
here's my receiver:
void eMailNotifikation_CheckedChanged(object sender, EventArgs e) {
CheckBox eMailNotification = (sender as CheckBox);
//Do some... calling a Webservice
eRoomWebservice.DoMyTasl(<params>);
}
the tables are only build with new... and ID.. nothing Special
EDIT::::
I've build the code in easy ASP.NET and there it is working, maybe this is a SharePoint Issue
protected void Page_Load(object sender, EventArgs e) {
Table test = new Table();
test.ID = "test1";
TableHeaderRow thr = new TableHeaderRow();
TableHeaderCell thc = new TableHeaderCell();
thc.Text = "Checkboxes";
thr.Controls.Add(thc);
test.Controls.Add(thr);
TableRow tr = new TableRow();
for (int i = 0; i < 10; i++)
{
TableCell tc = new TableCell();
CheckBox chb = new CheckBox();
chb.ID = "Some_" + i;
chb.AutoPostBack = true;
chb.CheckedChanged += new EventHandler(TestEH);
tc.Controls.Add(chb);
tr.Controls.Add(tc);
}
test.Controls.Add(tr);
root.Controls.Add(test);
}
void TestEH(object sender, EventArgs e) {
CheckBox chbx = sender as CheckBox;
string text = chbx.ID;
}
i found out wha'ts the Problem on my posted issue.
Content of the attribute ID="" was too Long, after shrinking the IDName it was working again.
Strange but it was the solution,
Remember: Watch out how Long Content of the Attribute ID is, there must be a Limit somewhere

Keeping GridView Column Width the Same

I am populating an ASP.NET GridView manually via C# code and have paging turned on. When a user goes from one page to another the column widths adjust accordingly to what is on the screen. I would like the GridView columns to remain the same no matter which page the user is on (preferably each column is set to the largest character length for its particular column). Can someone suggest the best course of action to accomplish this. Here is how I am populating the GridView
private void BindData(string sortExpression, string sortDirection)
{
db_set_connection_string.set_db_connnection_string(configuration_manager.GetConfigValue("DBSTRING.1"));
db_set_connection_string.set_individual_global_variables(configuration_manager.GetConfigValue("DBSTRING.1"));
sSQLResults = db_connector_mysql.execute_query_with_results_returned("SELECT * From city", false);
//Dynamically create columns
foreach (DataColumn column in sSQLResults.Columns)
{
//So we aren't getting duplicates, we need to check to see if it exists first
if (DoesColumnExistInGridView(column.ColumnName) == false)
{
BoundField nameColumn = new BoundField();
nameColumn.DataField = column.ColumnName;
nameColumn.HeaderText = column.ColumnName;
nameColumn.SortExpression = column.ColumnName;
GridView1.Columns.Add(nameColumn);
}
}
this.GridView1.DataSource = sSQLResults;
if (sortExpression != null)
{
sSQLResults.DefaultView.Sort = sortExpression + " " + sortDirection;
}
this.GridView1.DataBind();
}
There are two ways you can do this. Define width at the time of adding columns dynamically.
//Dynamically create columns
foreach (DataColumn column in sSQLResults.Columns)
{
//So we aren't getting duplicates, we need to check to see if it exists first
if (DoesColumnExistInGridView(column.ColumnName) == false)
{
BoundField nameColumn = new BoundField();
nameColumn.DataField = column.ColumnName;
nameColumn.HeaderText = column.ColumnName;
nameColumn.SortExpression = column.ColumnName;
nameColumn.Width=1;
GridView1.Columns.Add(nameColumn);
}
}
or you can do that in Gird View row data bind event
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e){
e.Rows.Cell[1].width=1;}

Handling empty databound dropdown list in ASP.Net

I have an asp.net page that has the following form fields:
Dropdown 1. This drop down receives the data via SQL data source.
Dropdown 2. Based on the selection of the drop down 1, this drop down queries to the database using SQL datasource and is populated.
A panel control that is placed below drop down list 2 and it has a set of the controls.
My problem is there may be a situation when nothing is returned from the datasource of drop down 2, I want to show an Item "No Data found" in the dropdown list 2 and simultaneously hide the panel that is placed below dropdown 2.
Can someone please let me know how this situation can be handled.
Appreciate the help.
Thanks,
Yagya
Add the following code to your dropdownlist1 selected index change event.
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
// Run this code when your sql datasource 2 does not return record, you can also place an IfElse condition
DataTable dt = new DataTable();
dt.Columns.Add("ID");
dt.Columns.Add("Value");
DataRow row = dt.NewRow();
row[0] = "-1";
row[1] = "Data Not Found";
dt.Rows.Add(row);
DropDownList2.DataSource = dt;
DropDownList2.DataTextField = "Value";
DropDownList2.DataValueField = "ID";
DropDownList2.DataBind();
}
Updated answer: ( Try this one )
You can also place it in your sqldatasource2 selecting event.
protected void SqlDataSource2_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
if (e.Arguments.TotalRowCount == 0)
{
DataTable dt = new DataTable();
dt.Columns.Add("ID");
dt.Columns.Add("Value");
DataRow row = dt.NewRow();
row[0] = "-1";
row[1] = "Data Not Found";
dt.Rows.Add(row);
DropDownList2.DataSource = dt;
DropDownList2.DataTextField = "Value";
DropDownList2.DataValueField = "ID";
DropDownList2.DataBind();
}
}
The above code will add a Item to your dropdownlist having Text = "Data Not Found"

Resources