Add multiple new rows in asp.net Repeater - asp.net

I'm developing an asp.net page that allows users to enter / edit multiple records. I am using a Repeater control to display the entry fields for each record. I would like to have an "Add" button that when clicked, inserts a new, blank set of entry fields. The user should be able to enter the new record, then click the add button multiple times and continue adding new records. Then, when the user clicks the Save button, those new records will be saved along with the changes to the existing records. If the user does not click Save, the records should be discarded and not written to the database.
Is there any way to implement this "add" functionality with the Repeater control?

There is a rather simple solution for this. Assuming you have some form of View Model that temporarily holds the data for the whatever the user is entering, you can simply add an additional "blank" ViewModel to the collection that the repeater uses as it's datasource. On save, you could simply save all the modifications to the database that exist in your View Model. A possible example would be:
protected void btnAdd_OnCommand(object sender, EventArgs e)
{
List<Item> items = (List<Item>)this.ItemRepeater.DataSource;
items.Add(new Item() { Id = -1 });
this.ItemRepeater.DataBind();
}
protected void btnSave_OnCommand(object sender, EventArgs e)
{
List<Item> items = (List<Item>)this.ItemRepeater.DataSource;
foreach (Item itm in items)
{
if (itm.Id == -1)
{
//Save New Item
}
else
{
//Update existing item
}
}
}
The major caveat here is that you must repopulate the ItemRepeater's datasource with the View Models updated with any changes the user made (that you would wish to keep/show).

Related

Posted data in gridview where I was added control dynamically

I added controls (i.e. template column) dynamically in grid view and click on button (post back) then grid view populated existing data (i.e.posted data) twice separated by
protected void Page_Init(object sender, EventArgs e)
{
//on init recreated controls
// if (IsPostBack)
{
gvFirst.DataSource = GetData("select top 10 * from Project_Master");
gvFirst.DataBind();
}
}
Sounds like you might be using Auto-generated fields. In design view, click the smart tag on the gridview and click "edit columns." Then uncheck the checkbox that says "Auto-generate fields." I think this should fix your problem if I am understanding you correctly.

How can I add a new dynamic control on button click?

When I click btnGDynamicCont I want to load the first set of controls, then on each further click of that button, add a new control (textbox) alongside the other ones, so each time it is clicked I am adding a new textbox across state.
Do you know where I should add the creation of the new textbox in order to keep it after each postback?
{
protected void Page_Load(object sender, EventArgs e)
{
if (Convert.ToString(ViewState["Generated"]) == "true")
GenerateDynamicControls();
}
public void GenerateDynamicControls()
{
TextBox txtDynamic = new TextBox();
txtDynamic.ID = "txtDynamic";
txtDynamic.Text = "Dynamic TextBox";
Page.Form.Controls.Add(txtDynamic);
TextBox txtDynamic2 = new TextBox();
txtDynamic2.ID = "txtDynamic2";
txtDynamic2.Text = "Dynamic Textbox";
Page.Form.Controls.Add(txtDynamic2);
}
protected void btnGDynamicCont_Click1(object sender, EventArgs e)
{
if (Convert.ToString(ViewState["Generated"]) != "true")
{
GenerateDynamicControls();
ViewState["Generated"] = "true";
}
else
{
Response.Write("<h2>Controls are already exist in page</h2>");
}
}
}
}
Dynamic controls are usually recreated at the Page_Load method. For more information, please refer to the Dynamically Created Controls in ASP.NET article.
You can refer the below link where a very similar issue is addressed.
unable to add more than one server control dynamically in asp.net
Everytime a postback happens, you should recreate the already existing controls(dynamically added) in your page_load event and the new controls are to be created in the button_click event.
Use some logic to generate ids for the controls for the viewstate to be maintained. VIEWSTATE will be taken care automatically if the ids of the controls generated before and after postback are the same.
One way to keep track of the number of textboxes is to store the count in session.

How to call a java script function in class of our asp.net webpage using c#?

I have used jQuery in my website. I have a navigation bar in my header. When user clicks any link given in my menu bar. It calls my javascript function..and a dialog box appears having a form. For example I have a form say "Add Country". When user clicks the link "Add country"
A form appears in a dialog box. And then user will be able to add country in the given form.
Now i have a gridview in my page..where im managing my data. My country table has 3 coloumns. Coutry Code, Country Name, Description. I have bound country table as sql datasource with that gridview. I have added a asp button as a template item in that gridview as well. All i want to do is..When user clicks the edit button of the particular row in my grid view..i would get the countryID of that row. And i want to call that country FORM again..where user will be able to edit. How can i call my java script function in c# code so that i would be able to call that form again ? I m relatively a new programmer. Anmy detailed guidance will be highly appreciated.
Lets suppose i have :
protected void EditSeriesButton(object sender, EventArgs e)
{
Button btn = (Button)sender;
GridViewRow row = (GridViewRow)btn.NamingContainer;
int countryID =Convert.ToInt32(CountryGridView.DataKeys[row.RowIndex].Value);
//Here i have got the id of that row. Now i want to call the function of my
//javascript so that i can call my country form.
//Just please guide me how to call javascript function here to open my
// dialog box.
//i will do rest of the things.
}
Please help me as soon as possible!!
Regards,
You can use gridview's rowdatabound event to add a onclick attribute to that button to call the javascript function:
protected void CountryGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int countryID = Convert.ToInt32(CountryGridView.DataKeys[e.Row.RowIndex].Value);
Button btnButton = (Button)e.Row.FindControl("btnName");
btnButton.OnClientClick = "return Dosomething('"+countryID+"');";
}
}

Get Value from Dynamic User Control On PostBack Asp.Net

I created an item user_control that has a textbox, button, etc. which will intentionally collect the total quantity of items the user wants.
I dynamically create a few intances of user_control on page_load. If you click the add button for the item quantity it will add to a session variable. however when the user enters a different quantity in the textbox and clicks the add button the total is of the original value of the textbox.
How to get the value the the user has typed in to the textbox to add to total???
You need to assign the same ID to the dynamic control after each postback, when you recreate the control. If you don't assign it the same ID, ViewState cannot populate the value.
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
TextBox txt = new TextBox();
txt.ID = "txt1";
PlaceHolder1.Controls.Add(txt);
}

Row Editing in the grid doesn't work in first click

I have a user control which contains a grid and three buttons for add,edit and delete.
I have placed this user control on an asp.net page.
I have OnClick events for these buttons.
When i click on add and delete buttons it's working fine but when i click on edit button,the onclick event of edit button is fired but the row in the grid doesn't appear in the edit mode, i have to click two times.
I don't know where is the problem.The onclick event handler for edit button is as follows:
protected void btnEditBankAccount_Click(object sender, EventArgs e)
{
grdBankAccounts.EditIndex = grdBankAccounts.SelectedIndex;
grdBankAccounts.RowSelectingEnabled = false;
}
Anyone please help.
my user control has a method which binds the grid to the data source, it's as follows
public void SetSupplierData(SupplierType Supplier)
{
if (Supplier != null)
{
ViewState["SupplierID"] = Supplier.SupplierId;
grdBankAccounts.DataSource = Supplier.BankAccounts;
grdBankAccounts.DataBind();
Session["BankAccounts"] = Supplier.BankAccounts;
}
}
the SetSupplierData method is called from the page where i have my user control.
In order to get this "in-place editing" in grids to work, I typically have to data-bind twice:
once in the OnInit or OnLoad method so that the button click event handlers have the data available to work on
in the OnPreRender method again to show the new values / new state (editing or not)
Marc

Resources