I am working on an e-commerce app and the problem is in showing the quantity of items purchased. The DropDownList shows the option to change quantity, it is in a gridview and it has a selectedindexchanged event. The event code works fine but when there are two(or more) products and we change the quantity of one row and then change the other rows's quantity, the previous row quantity is set back to default that is "1" while total price column shows the multiplication of price and quanity that we set to the DropDownList.
My DropDownList code:
<asp:TemplateField HeaderText="Qty">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:DropDownList ID="ddlQty" runat="server" AutoPostBack="True"
onselectedindexchanged="ddlQty_SelectedIndexChanged">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem>5</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
My C# code for the SelectedIndexChanged and GridView RowDataBoundevent:
protected void ddlQty_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlQty =(DropDownList)sender;
int rowindex = int.Parse(ddlQty.Attributes["rowindex"].ToString());
ViewState["index"] = rowindex;
ViewState["I"] = ddlQty.SelectedValue;
decimal price = Convert.ToDecimal(ds.Tables[0].Rows[rowindex]["Price"].ToString());
decimal totalprice = price * Convert.ToInt32(ddlQty.SelectedValue);
ds.Tables[0].Rows[rowindex]["TotalPrice"] = totalprice;
GridView1.DataSource = ds;
GridView1.DataBind();
ds.Tables[1].Rows[0]["TotalPrice"] = decimal.Parse(ds.Tables[1].Rows[0]["TotalPrice"].ToString()) + totalprice - Convert.ToDecimal(ds.Tables[0].Rows[rowindex]["Price"].ToString());
GridView1.FooterRow.Cells[2].Text = "Total Amount =";
GridView1.FooterRow.Cells[3].Text = ds.Tables[1].Rows[0]["TotalPrice"].ToString();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = (DropDownList)e.Row.FindControl("ddlQty");
ddl.Attributes.Add("rowindex", e.Row.RowIndex.ToString());
if (ViewState["index"] != null)
{
if (e.Row.RowIndex.ToString() == ViewState["index"].ToString())
{
ddl.SelectedValue = ViewState["I"].ToString();
}
}
}
}
I am attaching an image also to make it more clear.
I think your grid is binding on each postback made by dropdownlist, so changing one value will overwrite the formerly stored value in VS. You should try storing these values as a list, appending changes to it instead of replacing it each time.
IMHO, anyway, I would go by setting AutoPostBack properties to false, and updating row calculations client-side, then validating on server side on submit.
Related
I have Gridview with 10 Rows each row have 2 columns which is Textbox and Checkbox.
what i have to do is have to enter textbox Value as 1000 and i Clicked the Checkbox at first Row then value must go to Textbox of Second row and i clicked checkbox of Second Row then value must be go to third row of Textbox and so on.
i tried this,
protected void txtintroId_TextChanged(object sender, EventArgs e)
{
TextBox txt = (TextBox)sender;
GridViewRow grid = ((GridViewRow)txt.Parent.Parent.Parent);
TextBox txtIntro = (TextBox)txt.FindControl("txtintroId");
}
here i get the 1st row Value but How do i pass this to second Row.
Assist me
Here is full working code(as per your requirement )
Create a gridview like this
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%#Eval("txtcolumn") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True" OnCheckedChanged="CheckBox2_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Add a function in code behind C#
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
int rowNo=0;
foreach (GridViewRow GV1 in GridView1.Rows)
{
rowNo++;//
if (rowNo < GridView1.Rows.Count) //Checking that is this last row or not
{
//if no
CheckBox chkbx= (CheckBox)GV1.FindControl("CheckBox1");
TextBox curenttxtbx = (TextBox)GV1.FindControl("TextBox1");
if (chkbx.Checked == true )
`{
int nextIndex = GV1.RowIndex + 1;//Next row
TextBox txtbx = (TextBox)GridView1.Rows[nextIndex].FindControl("TextBox1");
txtbx.Text = curenttxtbx.Text;
}
}
//if yes
else
{
//For last row
//There is no next row
}
}
}
And i used this data table as data source
DataTable table = new DataTable();
table.Columns.Add("txtcolumn", typeof(string));
table.Rows.Add("1");
table.Rows.Add("32");
table.Rows.Add("32");
table.Rows.Add("32");
table.Rows.Add("3");
table.Rows.Add("32");
table.Rows.Add("32");
I tested this code it is working as per your requirement. (And SORRY for this bad formatting. i will do it later or please some body correct the formating :))
I have a GridView. Whenever I'm clicking on the LinkButton after selecting the Dropdown value, I'm getting only the first item of the Dropdown.
I need to show the selected value in TextBox. How can i get the desired value ?
This is my pseudo code:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" EnableModelValidation="True"
OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("ID")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Count">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" EnableViewState="true" AutoPostBack="false">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Button">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">LinkButton</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And the code behind:
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(string));
dt.Columns.Add("Count", typeof(string));
dt.Rows.Add("A", "5");
dt.Rows.Add("B", "8");
dt.Rows.Add("C", "4");
dt.Rows.Add("D", "7");
dt.Rows.Add("E", "9");
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList1");
ddl.Items.Clear();
int count = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "Count").ToString());
List<string> list = new List<string>();
for (int i = 1; i <= count; i++)
{
list.Add(i.ToString());
}
ddl.DataSource = list;
ddl.DataBind();
}
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
GridViewRow grdrow = (GridViewRow)((LinkButton)sender).NamingContainer;
DropDownList ddl = (DropDownList)GridView1.Rows[grdrow.RowIndex].FindControl("DropDownList1");
TextBox1.Text = ddl.SelectedValue.ToString();
}
Thanks in advance...
I'm going to guess that you should implement INotifyProperty change with the databinding. This way every change will be recorded including those made after first change in the drop down box. see ~ http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx
Since you are dynamically creating and binding the Dropdownlist, it would not have loaded at the point your click event is executed. For you to use bound properties (the selectedValue property in this case), the control must already be loaded and it's viewstate info parsed and set by the control.
The databinding event that creates the dropdownlist runs after pre-render. See http://msdn.microsoft.com/en-us/library/ms178472%28v=vs.85%29.aspx for details on the page lifecycle.
To do what you are asking requires very close attention to the page life cycle. Try moving the code that sets the textbox to the GridView1_RowDataBound method. For this to work though, your control must be rendered with the same properties everytime (that way the event's on it are wired up correctly and the selectedValue property is set correctly).
Another alternative is to handle events on the textbox or dropdownlist itself. You would again have to pay attention to the properties and lifecycle of the control.
ASP.Net Forms uses the SmartUI pattern and IMHO this is the single greatest knock against this pattern.
Ok, try this
protected void LinkButton1_Click(object sender, EventArgs e)
{
LinkButton lnkBtn = (LinkButton)sender;
GridViewRow row = (GridViewRow)lnkBtn.NamingContainer;
DropDownList ddl= (DropDownList)row.FindControl("DropDownList1");
TextBox1.Text = ddl.SelectedValue.ToString();
}
I am not able to get preselected text in dropdown in edit template. Please see my code:
<EditItemTemplate>
<asp:DropDownList ID="droplist" runat="server">
</asp:DropDownList>
</EditItemTemplate>
c# code
protected void gvDetails_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
DropDownList droplist = (DropDownList)e.Row.FindControl("droplist");
droplist.DataSource = EquipmentBLL.getunitdrop();
droplist.DataTextField = "UnitName";
droplist.DataValueField = "UnitID";
droplist.DataBind();
droplist.Items.Insert(0, new ListItem(" Select Unit ", "0"));
//droplist.Items.FindByText(unittypetext).Selected = true;
}
}
}
Can someone tell me what I should do to get preselected dropdown?
regards
Hussain
Right now, you're populating the DropDownList with options from a datasource. However, you're not binding it's selected value to anything.
Whatever you are doing to bind the other fields in your Gridview, do that for the DropDownList's SelectedValue as well.
Without seeing the rest of your GridView markup, I'm thinking something like this should work:
<EditItemTemplate>
<asp:DropDownList ID="droplist" runat="server"
SelectedValue='<%# Bind("UnitID") %>' >
</asp:DropDownList>
</EditItemTemplate>
Where "UnitID" above is the name of the field from your GridView's datasource that you want to bind to the SelectedValue of the DropDownList.
I have a status drop down list which I am using within a grid view, the users can select a list item they want when editing a row within the grid view. The problem is that the current implementation is not retrieving the selected value, but is only retrieving the default/loaded value.
This is the defination of the grid view:
<asp:GridView ID="applicationGrid" runat="server" Width="95%"
AutoGenerateEditButton="True"
AutoGenerateColumns="False"
ShowFooter="True"
CellSpacing="10"
HeaderStyle-HorizontalAlign="Left"
ItemStyle-HorizontalAlign="Left"
OnRowUpdating="applicationGrid_RowUpdating"
OnRowEditing="applicationGrid_RowEditing"
OnRowCancelingEdit="applicationGrid_RowCancelingEdit"
AutoPostBack="true" >
And this is the defination of the Column where the dropdown list will appear on edit:
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:Label ID="StatusDescription" runat="server" Text='<%# Eval("STATUS_DESCRIPTION") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="StatusDescriptionList" runat="server" DataTextField="status_description"
DataValueField="application_status_code" OnLoad="DropDownLoadEdit">
<asp:ListItem Text="Status:" Value="default"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
This is the code behind which is handling the update scenario:
protected void applicationGrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = applicationGrid.Rows[e.RowIndex];
applicationGrid.EditIndex = -1;
Label applicationCodeLabel = row.FindControl("AppID") as Label;
TextBox applicationNameTextBox = row.FindControl("AppNameEdit") as TextBox;
TextBox applicationURLTextBox = row.FindControl("AppURLEdit") as TextBox;
DropDownList applicationStatusDropDownList = row.FindControl("StatusDescriptionList") as DropDownList;
int applicationCode = Convert.ToInt32(applicationCodeLabel.Text);
string applicationName = applicationNameTextBox.Text;
string applicationURL = applicationURLTextBox.Text;
int applicationStatus = Convert.ToInt32(applicationStatusDropDownList.SelectedValue.ToString());
//string applicationStatus2 = applicationStatusDropDownList.SelectedItem.Value;
//string applicationStatus3 = applicationStatusDropDownList.SelectedItem.Text;
application.UpdateApplication(applicationCode, applicationName, applicationURL);
PopulateApplications();
}
All is working, but the selected value is not the one which the is loaded and not the one which the user selects. Therefore the problem is getting the selected value from the list. What needs to be changed, and why?
protected void applicationGrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = applicationGrid.Rows[e.RowIndex];
applicationGrid.EditIndex = -1;
Label applicationCodeLabel = row.FindControl("AppID") as Label;
TextBox applicationNameTextBox = row.FindControl("AppNameEdit") as TextBox;
TextBox applicationURLTextBox = row.FindControl("AppURLEdit") as TextBox;
DropDownList applicationStatusDropDownList = row.FindControl("StatusDescriptionList") as DropDownList;
int applicationCode = Convert.ToInt32(applicationCodeLabel.Text);
string applicationName = applicationNameTextBox.Text;
string applicationURL = applicationURLTextBox.Text;
int applicationStatus = Convert.ToInt32(applicationStatusDropDownList.SelectedValue.ToString());
//string applicationStatus2 = applicationStatusDropDownList.SelectedItem.Value;
//string applicationStatus3 = applicationStatusDropDownList.SelectedItem.Text;
application.UpdateApplication(applicationCode, applicationName, applicationURL);
PopulateApplications();
}
EDIT: Adding my Populate Methods:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
try
{
PopulateApplications();
}
catch (Exception exception)
{
throw exception;
}
}
}
private void PopulateApplications()
{
DataTable reader = application.GetApplicationList();
applicationGrid.DataSource = reader;
applicationGrid.DataBind();
applicationGrid.AllowSorting = true;
}
protected void DropDownLoadEdit(object sender, EventArgs e)
{
DataTable statusTable = application.GetStatusList();
DropDownList dropdown = sender as DropDownList;
dropdown.DataSource = statusTable;
dropdown.DataTextField = "status_description";
dropdown.DataValueField = "application_status_code";
dropdown.DataBind()
}
Update #2: I am trying to fill up a static variable in the class which is for the selected index. This will then be used when update is pressed. However, this is still getting the original value of the drop down list and not the selected one.
This is the method:
protected void StatusDescriptionList_SelectedIndexChanged(object sender, System.EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
selectedValue = Convert.ToInt32(ddl.SelectedValue.ToString());
}
Are you repopulating the list in DropDownLoadEdit, regardless of whether the transition is a postback or not? If so, the list will be repopulated before its value is read, and set to the default before your method has a chance to read the value.
The issue may be of the post back.
Once the item is selected from the drop down the page get post back the value you selected get vanished.
To over come this issue. Bind the drop down list inside page is post back or else make use of a Ajax update panel.
#Ryan, Have you done this
'
AutopostBack="True"
<asp:DropDownList ID="StatusDescriptionList" runat="server" AutopostBack="True" DataTextField="status_description"
DataValueField="application_status_code" OnLoad="DropDownLoadEdit">
<asp:ListItem Text="Status:" Value="default"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="StatusDescriptionList" runat="server" DataTextField="status_description"
DataValueField="application_status_code" SelectedValue="application_status_code" AutopostBack="True" OnLoad="DropDownLoadEdit">
<asp:ListItem Text="Status:" Value="default"></asp:ListItem>
</asp:DropDownList>
I want to add a dropdownlist to every entry in a gridview.
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False"
onselectedindexchanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:TemplateField HeaderText="Bank">
<ItemTemplate>
<asp:DropDownList ID="DropDown"
AutoPostBack="true" runat="server" DataTextField="Name" DataValueField="Name"
>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
At the back end i have the following code in order to bind a datatable to that dropdown list.
DataTable reader = BusinessLayer.BusinessLayerHandler.GetBankList();
DropDown.DataSource = reader;
DropDown.DataTextField = "NAME";
DropDown.DataValueField = "NAME";
DropDown.DataBind();
My problem is that the drop down list created at the grid view (DropDown) is not found at the back end as if it doesn't exist..
What can I do?
The DropDownList will be created for every single item in the GridView, so there can't be one field for the dropdownlists. Nevertheless, you can retrieve the DropDownList for a single row (e.g. in RowDataBound or RowCreated event)
protected void grid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(r.Row.RowType == DataControlRowType.DataRow)
{
DropDownList dropdown = e.Row.FindControl("DropDown") as DropDownList;
if(dropdown != null)
{ /* your code */ }
}
}
Or you can use an event of the DropDownList itself and access the sender parameter.
<asp:DropDownList ID="DropDown" OnLoad="dropdownLoad" />
protected void dropdownLoad(object sender, EventArgs e)
{
DropDownList dropdown = sender as DropDownList;
if(dropdown != null)
{ /* your code */ }
}
you can find dropdown into grid databound event by grid.findcontrol.