LinkButton in GridView not picking correct value - asp.net

I am using link button to display a pdf report for selected Report Id from Grid view. On page load I am binding the datasource with Gridview. By clicking the link button it fetches the correct report id from column 0 and displays the report. After sorting the grid the grid view shows the sorted data. Now if i click the link button it is not picking the changed value from column 0. instead it is picking the value before sorted.
Say: Column 0 has values 1, 2, 3, 4, 5 before sorting. If i click link button of Row 3 before sorting it is picking value 3 from column 0. After sorting it is 3,4,5,2,1. Now if i click link button of row 3 it is still picking value 3 instead of 5. Can you please help me.
Below is my code:
aspx:
CS:
protected void Page_Load(object sender, EventArgs e)
{
bindGrid();
}
protected void GvStockTakingReport_Sort(object sender, GridViewSortEventArgs e)
{
GvStockTakingReport.DataSource = null;
GvStockTakingReport.DataBind();
GvStockTakingReport.Dispose();
DataSet ds = StockTakingList.BindStocktakingReportGrid();
DataTable dtSortTable = ds.Tables[0];
if (dtSortTable != null)
{
DataView dv = new DataView(ds.Tables[0]);
dv.Sort = e.SortExpression + " " + getSortDirectionString();
//ViewState["sortExpression"] = e.SortExpression;
//Session["sortExpression"] = e.SortExpression;
GvStockTakingReport.DataSource = dv;
GvStockTakingReport.DataBind();
}
}
protected void PrintReport(object sender, CommandEventArgs e)
{
LinkButton lkButton = (LinkButton)sender;
GridViewRow item = (GridViewRow)lkButton.NamingContainer;
string Id = (item.FindControl("lblstk_id") as Label).Text;
//Getting value for Id
string Id1 = e.CommandArgument.ToString();
int rowIndex = Convert.ToInt32((string)e.CommandArgument);
//string Id1 = Convert.ToString(GvStockTakingReport.Rows[rowIndex].Cells[0].Text);
string Id2 = ((Label)(GvStockTakingReport.Rows[rowIndex].FindControl("lblstk_id"))).Text;
//Getting value for Id
string virtualPath = string.Format("~/{0}/{1}{2}", Portal.Business, Portal.Core.Profile.ReportsDirectory, "General/RPT_03002_StockTakinReport.rpt");
string physicalPath = Server.MapPath(virtualPath);
using (ReportDocument report = new ReportDocument())
{
report.Load(physicalPath);
Core.Security.CrystalReportLogOn(report, (SqlConnectionStringBuilder)Portal.Core.Profile.ConnectionStrings["MTServer"]);
report.SetParameterValue("#STK_Id", Id);
report.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Page.Response, true, "Stock Taking report");
}
}

the issue was whenever i click the link button the page load calls
bindgrid method which resets the gridview to original sorting before the printreport method invokes.
I have included the sorting logic in bindgrid method as well as below which resolved the issue.
public void bindGrid()
{
try
{
DataSet ds = StockTakingList.BindStocktakingReportGrid();
GvStockTakingReport.DataSource = ds;
GvStockTakingReport.DataBind();
//To make sure the grid remains with sorting order while pageload method invokes otherwise it will reset to original order
DataView dv = new DataView(ds.Tables[0]);
if (dv != null)
{
if (ViewState["sortExpression"] == null)
{
ViewState["sortExpression"] = "STK_Id"; //First time it will sort by STK_ID i.e on first time page loading
}
if (ViewState["sortDirection"] == null)
{
ViewState["sortDirection"] = "ASC";//First time it will sort ascending i.e on first time page loading
}
dv.Sort = ViewState["sortExpression"].ToString() + " " + ViewState["sortDirection"].ToString();
GvStockTakingReport.DataSource = dv;
GvStockTakingReport.DataBind();
}
}
catch (Exception ex)
{
error.Visible = true;
error.Text = ex.Message.ToString();
}
}

Related

GridVIew does not refresh after paging

My GridView is populated with a certain filter criteria.
I have PageIndexChanging event to perform pagination of my data.
protected void gvPeople_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
lblAddError.Text = String.Empty;
List<People> list = (List<People>)ViewState[_vsPeopleList];
gvPeople.DataSource = list;
gvPeople.PageIndex = e.NewPageIndex;
gvPeople.EditIndex = -1;
gvPeople.DataBind();
}
After that I perfrom Search without changing the filter criteria.
protected void btnPeopleSearch_Click(object sender, CommandEventArgs e)
{
if(e.CommandName.Equals("Search"))
{
PopulatePeople();
}
}
private void PopulatePeople()
{
lblAddError.Text = String.Empty;
if (ViewState["_message"] != null)
{
lblAddError.Visible = true;
lblAddError.Text = ViewState["_message"].ToString();
}
ViewState["_message"] = null;
int portfolio = int.Parse(ddlPortfolio.SelectedItem.Value);
ViewState["portfolioID"] = portfolio;
string year = ddlYear.SelectedItem.Text;
string month = ddlMonth.SelectedItem.Text;
List<People> list = People.GetPeople(portfolio, year, month);
ViewState[_vsPeopleList] = list;
if(list.Count == 0)
{
gvPeople.Visible = false;
lblAddError.Visible = true;
lblAddError.Text = "No data available for current selection";
}
else
{
gvPeople.Visible = true;
gvPeople.DataSource = list;
gvPeople.DataBind();
}
}
I'm supposed to get all the data in the GridView as I had before paging to the next page. However, my page does not refresh to show all the data, it only shows the data for that page index, however, my list has count equals to the number of records populated from the database. Also, if I change search filter, data is displayed correctly.
What am I doing wrong?
Looks like the gvHolidays_PageIndexChanging event is related to some other grid as the code written within it is for grid gvPeople.
If not, then try the following.
//Write the following code inside gridviews PageIndexChanging event.
gvPeople.PageIndex = e.NewPageIndex;
gvPeople.EditIndex = -1;
gvPeople.SelectedIndex = -1;
//Write the following code inside gvHolidays_PageIndexChanged event.
lblAddError.Text = String.Empty;
List<People> list = (List<People>)ViewState[_vsPeopleList];
gvPeople.DataSource = list;
gvPeople.DataBind();

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.

Gridview paging Show all Records

i have a gridview that has paging enabled. i also have a dropdown on the pager of the gridview where the user can select how many records per page they would like to retrieve. Once the dropdown is changed then an event is fired (shown below) to rerun the query with the updated results per page request. This works very well. I did however want to have a value "All" on the dropdown aswell and the method i used to impliment this is by disabling paging.
This all works brilliantly except for one issue. When the user selects "All" on the dropdown i would like to still show the pager once the gridview is updated. It doesnt show because i turned off paging but is there a way to show the pager again? See my code below for the event. (As you can see i renable the pager at the end but this has no effect)
thanks
damo
Code behind Event for Dropdown Change
void GridViewMainddl_SelectedIndexChanged(object sender, EventArgs e)
{
//changes page size
if ((((DropDownList)sender).SelectedValue).ToString() == "All")
{
GridViewMain.AllowPaging = false;
}
else
{
GridViewMain.PageSize = int.Parse(((DropDownList)sender).SelectedValue);
}
//binds data source
Result fAuditOverallStatusLatest = new Result(sConn);
GridViewMain.DataSource = Result.getAuditOverallStatusLatest();
GridViewMain.PageIndex = 0;
GridViewMain.DataBind();
GridViewMain.AllowPaging = true;
GridViewMain.BottomPagerRow.Visible = true;
GridViewMain.TopPagerRow.Visible = true;
}
DDL Code behind
protected void GridViewMain_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Pager)
{
DropDownList GridViewMainddl = new DropDownList();
//adds variants of pager size
GridViewMainddl.Items.Add("5");
GridViewMainddl.Items.Add("10");
GridViewMainddl.Items.Add("20");
GridViewMainddl.Items.Add("50");
GridViewMainddl.Items.Add("100");
GridViewMainddl.Items.Add("200");
GridViewMainddl.Items.Add("500");
GridViewMainddl.Items.Add("All");
GridViewMainddl.AutoPostBack = true;
//selects item due to the GridView current page size
ListItem li = GridViewMainddl.Items.FindByText(GridViewMain.PageSize.ToString());
if (li != null)
GridViewMainddl.SelectedIndex = GridViewMainddl.Items.IndexOf(li);
GridViewMainddl.SelectedIndexChanged += new EventHandler(GridViewMainddl_SelectedIndexChanged);
//adds dropdownlist in the additional cell to the pager table
Table pagerTable = e.Row.Cells[0].Controls[0] as Table;
TableCell cell = new TableCell();
cell.Style["padding-left"] = "15px";
cell.Controls.Add(new LiteralControl("Page Size:"));
cell.Controls.Add(GridViewMainddl);
pagerTable.Rows[0].Cells.Add(cell);
//add current Page of total page count
TableCell cellPageNumber = new TableCell();
cellPageNumber.Style["padding-left"] = "15px";
cellPageNumber.Controls.Add(new LiteralControl("Page " + (GridViewMain.PageIndex + 1) + " of " + GridViewMain.PageCount));
pagerTable.Rows[0].Cells.Add(cellPageNumber);
}
}
Put this in your Page_Init:
GridViewMain.PreRender += new EventHandler(GridViewMain_PreRender);
Then elsewhere in your Page class:
void GridViewMain_PreRender(object sender, EventArgs e)
{
var pagerRow = (sender as GridView).BottomPagerRow;
if (pagerRow != null)
{
pagerRow.Visible = true;
}
}
Then for your drop down event:
void GridViewMainddl_SelectedIndexChanged(object sender, EventArgs e)
{
MyServices fServices = new FAServices(sConn);
Result fAuditOverallStatusLatest = new Result(sConn);
var data = Result.getAuditOverallStatusLatest();
//changes page size
if ((((DropDownList)sender).SelectedValue).ToString() == "All")
{
GridViewMain.PageSize = data.Count();
}
else
{
GridViewMain.PageSize = int.Parse(((DropDownList)sender).SelectedValue);
}
//binds data source
GridViewMain.DataSource = data;
GridViewMain.PageIndex = 0;
GridViewMain.DataBind();
GridViewMain.AllowPaging = true;
}
In that PreRender event, you'll have to duplicate that code for the top pager.
Edit: To get All selected, make this change in GridViewMain_RowCreated:
if (li != null)
{
GridViewMainddl.SelectedIndex = GridViewMainddl.Items.IndexOf(li);
}
else
{
GridViewMainddl.SelectedIndex = GridViewMainddl.Items.Count - 1;
}

How to add an extra row (containing a Button and corresponding event handler) to the GridView

This must be something that a lot of people have done. Basically, it's a custom GridView (i.e. inherited control) with the ability to update all rows at once. I've tried putting the "update all" button in various places (footer, pager, outside the grid), but it looks neatest (to me) when the button is in an extra row as the last row of the GridView.
NB: The pager row is not a suitable place for this button because this custom control could be used in a situation where paging is false. Similarly, the normal footer may be required for some other purpose (e.g. totals).
Here's my code for putting the button in the correct place (with apologies for the terse variables etc.):
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
//Add an extra row to the table...
if (_updateAllEnabled)
{
GridViewRow r = base.CreateRow(-1, -1, DataControlRowType.Footer, DataControlRowState.Normal);
Button btn = new Button();
TableCell c = new TableCell();
btn.ID = "UpdateAllButton"; // tried with and without this line
btn.Text = "Update All";
btn.Click += new EventHandler(UpdateAll);
r.Cells.Add(c);
c.Controls.Add(btn);
Table t = this.Controls[0] as Table;
c.ColumnSpan = this.Columns.Count;
t.Rows.Add(r);
}
}
This gives the appearance that I want, but the click event (UpdateAll) does not fire.
I assume that the stuff is being added too late in the life cycle (PreRender), but where else can I do this to ensure that the row is at the end of the GridView? I also thought that there might be trouble identifying the button, so I tried setting the ID. In any case, the ID in the generated HTML looks OK (consistent with "working" buttons in the pager row.
Is there a way for me to achieve this or am I attempting the impossible?
The best place to create your footer-controls is RowCreated since that's early enough in the lifecycle and also ensures that their recreated on every postback:
Footer approach:
protected void Grid_RowCreated(Object sender, GridViewRowEventArgs e) {
if(e.Row.RowType == DataControlRowType.Footer) {
Button btn = new Button();
TableCell c = new TableCell();
btn.ID = "UpdateAllButton";
btn.Text = "Update All";
btn.Click += new EventHandler(UpdateAll);
var firstCell=e.Row.Cells[0];
firstCell.ColumnSpan =e.Row.Cells.Count;
firstCell.Controls.Add(btn);
while(e.Row.Cells.Count > 1)e.Row.Cells.RemoveAt(e.Row.Cells.Count-1);
}
}
Of course you have to set ShowFooter to true:
<asp:GridView ID="GridView1"
ShowFooter="true"
OnRowCreated="Grid_RowCreated"
runat="server"
</asp:GridView>
Pager approach:
In my opinion this is the purpose of the FooterRow. But if you really want to ensure that your Button is in the very last row of a GridView(even below Pager as commented), i would try my next approach.
Here I'm using the pager for your costom control(s) by adding another TableRow to the PagerTable which inherits from Table.
protected void Grid_RowCreated(Object sender, GridViewRowEventArgs e) {
switch(e.Row.RowType){
case DataControlRowType.Pager:
Button btnUpdate = new Button();
btnUpdate.ID = "UpdateButton";
btnUpdate.Text = "Update";
btnUpdate.Click += new EventHandler(UpdateAll);
var tblPager = (Table)e.Row.Cells[ 0 ].Controls[ 0 ];
var row = new TableRow();
var cell = new TableCell();
cell.ColumnSpan = tblPager.Rows[ 0 ].Cells.Count;
cell.Controls.Add(btnUpdate);
row.Cells.Add(cell);
tblPager.Rows.Add(row);
break;
}
}
To ensure that the pager is visible even if only one page is shown(note that the real pager is invisible if PageSize==1):
protected void Grid_PreRender(object sender, EventArgs e){
GridView gv = (GridView)sender;
GridViewRow gvr = (GridViewRow)gv.BottomPagerRow;
if(gvr != null) {
gvr.Visible = true;
var tblPager = (Table)gvr.Cells[ 0 ].Controls[ 0 ];
//hide real pager if unnecessary
tblPager.Rows[ 0 ].Visible = GridView1.PageCount > 1;
}
}
Of course now you have to set AllowPaging=true:
<asp:GridView ID="GridView1"
AllowPaging="true"
PagerSettings-Mode="NumericFirstLast"
OnRowCreated="Grid_RowCreated"
OnPreRender="Grid_PreRender"
OnPageIndexChanging="Grid_PageChanging"
runat="server">
</asp:GridView>
Final approach(working for a custom GridView and all PagerPositions):
public PagerPosition OriginalPagerPosition{
get { return (PagerPosition)ViewState[ "OriginalPagerPosition" ]; }
set { ViewState[ "OriginalPagerPosition" ] = value; }
}
protected void Page_Load(object sender, EventArgs e){
if(!IsPostBack) OriginalPagerPosition = GridView1.PagerSettings.Position;
GridView1.PagerSettings.Position = PagerPosition.TopAndBottom;
GridView1.AllowPaging = true;
// databinding stuff ...
}
Keep the RowCreated the same as above in Pager approach.
Visibility of top/bottom pagers will be controlled in PreRender according to the OriginalPagerPosition property. Both pagers are created even with PagerPosition=TOP, the bottom pager is required for your additional control(s):
protected void Grid_PreRender(object sender, EventArgs e)
{
GridView gv = (GridView)sender;
GridViewRow tpr = (GridViewRow)gv.TopPagerRow;
GridViewRow bpr = (GridViewRow)gv.BottomPagerRow;
tpr.Visible = gv.PageCount > 1 && (OriginalPagerPosition == PagerPosition.Top || OriginalPagerPosition == PagerPosition.TopAndBottom);
bpr.Visible = true;
var tblBottomPager = (Table)bpr.Cells[ 0 ].Controls[ 0 ];
tblBottomPager.Rows[ 0 ].Visible = gv.PageCount > 1 && (OriginalPagerPosition == PagerPosition.Bottom || OriginalPagerPosition == PagerPosition.TopAndBottom);
var tblTopPager = (Table)tpr.Cells[ 0 ].Controls[ 0 ];
tblTopPager.Rows[1].Visible = false;
}
Note: if you are extending the GridView control, you have to replace all occurences of GridView1(my test-grid) with this.
It would be easy to add an extra row into the grid. But the difficulty in your requirement is that the GridView's RowCollection should not contain this row since that would be error-prone. It should also be the very last row even if paging is enabled. This is (afaik) not possible.
Hence i've chosen to extend the pager with this functionality.
I'll add this as separate answer since my other is already too detailed and describes two different ways(footer,pager) to add controls to a GridView without extending it.
This approach extends a GridView as in your own requirement and is similar to my other pager-approach. But it's cleaner and only adds the additional row to the BottomPager. It woks also for every setting(AllowPaging=false,Pager-Position: Top,Bottom,BottomTop):
[DefaultProperty("EnableUpdateAll")]
[ToolboxData("<{0}:UpdateGridView runat=server></{0}:UpdateGridView>")]
public class UpdateGridView : GridView
{
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("true")]
[Localizable(true)]
public bool EnableUpdateAll
{
get
{
Object val = ViewState["EnableUpdateAll"];
return ((val == null) ? true : (bool)val);
}
set
{
ViewState["EnableUpdateAll"] = value;
}
}
private bool OriginalAllowPaging
{
get
{
Object val = ViewState["OriginalAllowPaging"];
return (bool)val;
}
set
{
ViewState["OriginalAllowPaging"] = value;
}
}
private PagerPosition OriginalPagerPosition
{
get
{
Object val = ViewState["OriginalPagerPosition"];
return (PagerPosition)val;
}
set
{
ViewState["OriginalPagerPosition"] = value;
}
}
protected override void OnInit(System.EventArgs e)
{
if (ViewState["OriginalPagerPosition"] == null)
OriginalPagerPosition = base.PagerSettings.Position;
if(OriginalPagerPosition != PagerPosition.Bottom)
PagerSettings.Position=PagerPosition.TopAndBottom;
if (ViewState["OriginalAllowPaging"] == null)
OriginalAllowPaging = base.AllowPaging;
base.AllowPaging = true;
}
protected override void OnRowCreated(GridViewRowEventArgs e)
{
switch (e.Row.RowType)
{
case DataControlRowType.Pager:
//check if we are in BottomPager
if (this.Rows.Count != 0 && this.EnableUpdateAll)
{
Button btnUpdate = new Button();
btnUpdate.ID = "BtnUpdate";
btnUpdate.Text = "Update";
btnUpdate.Click += new EventHandler(UpdateAll);
var tblPager = (Table)e.Row.Cells[0].Controls[0];
var row = new TableRow();
var cell = new TableCell();
cell.ColumnSpan = tblPager.Rows[0].Cells.Count;
cell.Controls.Add(btnUpdate);
row.Cells.Add(cell);
tblPager.Rows.Add(row);
}
break;
}
}
protected override void OnPreRender(EventArgs e)
{
bool bottomPagerVisible =
OriginalAllowPaging &&
PageCount > 1 &&
(OriginalPagerPosition == PagerPosition.Bottom || OriginalPagerPosition == PagerPosition.TopAndBottom);
BottomPagerRow.Visible = bottomPagerVisible || EnableUpdateAll;
var tblBottomPager = (Table)BottomPagerRow.Cells[0].Controls[0];
tblBottomPager.Rows[0].Visible = bottomPagerVisible;
}
private void UpdateAll(Object sender, EventArgs e)
{
// do something...
}
}

Create Dynamic Textbox and Get values

I want to create dynamic text box when user click on Add more link button.
For this I am using this code. And I have to mention that I am using master page.
protected void lnkAddMore_Click(object sender, EventArgs e)
{
if (Request.Cookies["value"] != null)
{
i = Convert.ToInt32(Request.Cookies["value"].Value) + 1 ;
}
for (int k = 1; k <= i; k++)
{
LiteralControl literal = new LiteralControl();
literal.Text = "<br /><br />";
Label newLabel = new Label();
newLabel.Text = "Choice" + " " + k.ToString();
newLabel.ID = "lblChoice_" + k.ToString();
newLabel.Attributes.Add("runat", "Server");
this.panelLabel.Controls.Add(newLabel);
this.panelLabel.Controls.Add(literal);
LiteralControl literal1 = new LiteralControl();
literal1.Text = "<br /><br />";
TextBox nexText = new TextBox();
nexText.ID = "txtChoice_" + k.ToString();
nexText.Attributes.Add("TextMode", "MultiLine");
nexText.Attributes.Add("runat", "Server");
panelTextbox.Controls.Add(nexText);
this.panelTextbox.Controls.Add(literal1);
Response.Cookies["value"].Value = i.ToString();
Session["Panel"] = panelTextbox;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["Panel"] != null)
{
ContentPlaceHolder content=new ContentPlaceHolder();
content.Controls.Add(Session["Panel"] as Panel);
}
}
}
Now I am facing trouble how to retrieve the data of the these text boxes after the clicking on the submit button so that I can store the values of there text boxes to database.
What will be code written for the click event of btnSave
protected void btnSave_Click(object sender, EventArgs e)
{
if (Session["Panel"] != null)
{
ContentPlaceHolder content_new = new ContentPlaceHolder();
for (int i = 1; i <= count; i++)
{
strControlName = "txtChoice_" + i.ToString();
TextBox objTextBox = (TextBox)content_new.FindControl(strControlName);
strTextBoxValues[i] = objTextBox.Text;
string str3 = strTextBoxValues[2];
}
}
}
This code is showing error for objTextBox. The error is NullReferenceException.
How to write stored procedure for saving data of above code?
The main problem is handling the parameter declaration, how to declare dynamic parameter for passing values so that value is saved for dynamic textbox?
Thanks.
I have already answered it here.
Lost dynamically created text box values
You can try this.
private string GetValue(string ControlID)
{
string[] keys = Request.Form.AllKeys;
string value = string.Empty;
foreach (string key in keys)
{
if (key.IndexOf(ControlID) >= 0)
{
value = Request.Form[key].ToString();
break;
}
}
return value;
}
Then to get the value
string txtChoice1value = GetValue("txtChoice1");
First of all when you dynamically create a control it doesn't need to be set "runat = sever".
Problem is in this line `ContentPlaceHolder content_new = new ContentPlaceHolder();` you make a new ContentPlaceHolder, this mean it doesn't have any control to be found.
Check this page. How To Create TextBox Control Dynamically at Runtime
You need to find the reference of your already created ContentPlaceHolder like-
ContentPlaceHolder cnt =(ContentPlaceHolder)this.Master.FindControl("ContentPlaceHolder1");
and then add the dynamically created Control in that ContentPlaceHolder as-
cnt.Controls.Add(Session["Panel"] as Panel);
Why you creating a new ContentPlaceHolder each time even when you have mentioned that you are using masterPage, so there must exists a ContentPlaceHolder..
Controls wont persist on postback have a look at http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx

Resources