I'm trying to build some handling for OnRowUpdating in my GridView gvTaskDashboard. I am wanting to check that if a Status dropdown list has Complete selected, that a date has been entered into the Complete Date textbox. I'm sure the problem is in my if statement trying to test for Complete, but because the dropdown is bound to the index of a sql table, I'm not sure how to check for the text value of the selected index. I'm sure I could get by with using if (ddlStatus.SelectedValue == "5") for example, but as a coder, I would think checking for "Complete" makes more sense from a maintenance standpoint.
Markup for DDL
<asp:DropDownList ID="ddlStatus" runat="server" AutoPostBack="True" DataSourceID="ddlStatusSQL"
DataTextField="TaskStatusName" DataValueField="TaskStatusID" SelectedValue='<%# Bind("TaskStatusID") %>'>
</asp:DropDownList>
SQL DataSource for DDL
<asp:SqlDataSource ID="ddlStatusSQL" runat="server" ConnectionString="<%$ ConnectionStrings:ProWorxConnectionString %>"
SelectCommand="SELECT [TaskStatusID], [TaskStatusName] FROM [tblTaskStatus]
ORDER BY [TaskStatusType], [TaskStatusName]">
</asp:SqlDataSource>
CodeBehind
protected void gvTask_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow gvr = gvTaskDashboard.Rows[e.RowIndex];
// Get Controls
DropDownList ddlStatus = (DropDownList)gvr.FindControl("ddlStatus");
TextBox txtCompDate = (TextBox)gvr.FindControl("txtCompDate");
// Test for Complete Date if Complete status selected
if (string.IsNullOrWhiteSpace(txtCompDate.Text) && ddlStatus.SelectedValue == "Complete")
{
// Throw Error
ClientScript.RegisterStartupScript(GetType(), "error", "alert('Enter Complete Date if Status is Complete.');", true);
}
else { TaskDashboardSQL.Update(); }
}
Hi give this a whirl:
ddlStatus.SelectedItem.Text == "Complete"
Related
I have multiple dropdowns like this
<asp:DropDownList ID="ddl1"
runat="server"
DataTextField="Text"
DataValueField="ValID"
AutoPostBack="true"
OnSelectedIndexChanged="ddl_SelectedIndexChanged">
</asp:DropDownList>
<asp:DropDownList ID="ddl2"
runat="server"
DataTextField="Text"
DataValueField="ValID"
AutoPostBack="true"
OnSelectedIndexChanged="ddl_SelectedIndexChanged">
</asp:DropDownList>
Both the dropdowns has different set of values they are unique. I did the following on ddl_SelectedIndexChanged funtion
DropDownList ddl = sender as DropDownList;
string selectedId = ddl.ID;
string selectedText = ddl.SelectedItem.Text;
When I select values on the first dropdown I get correct selectedId and selectedText.
My Problem:
When I select the second dropdown the selectedId is of the second dropdown but selectedText is always the first dropdowns first value and it never changes. I need selectedText to be that of the Item which I select in the second dropdown.
Any Suggestion?
When binding the data to the DropDownLists, you must place them inside an IsPostBack check.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ddl1.DataSource = source;
ddl1.DataBind();
ddl2.DataSource = source;
ddl2.DataBind();
}
}
If you do not the SelectedIndexChanged will fire each time for the DropDownList because rebinding data will trigger that since the previous selecedindex is overwritten.
I have a simple checkBox in Editable gridView :
<asp:TemplateField HeaderText="Editable">
<ItemTemplate>
<asp:Label runat="server" Text="<%# Item.IsEditable %>" />
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="CheckBoxEditable " runat="server" Text="Editable"></asp:CheckBox>
</EditItemTemplate>
</asp:TemplateField>
When I click on edit button in the row, I would like that checkBox is already checked if value is true. (IsEditable is a boolean)
Text Field is easy because I have a BindItem on Text property in EditItemTemplate. But it's not the same for checkBox or dropdownlist
GridView
I use a UpdateItem Method to update my data in database. I tried a small condition to check my checkBox but it does'nt work.
public void GridViewRisquesAggravants_UpdateItem(IndexViewModel item)
{
try
{
if (ModelState.IsValid)
{
CheckBox chbEdit = (CheckBox)GridView.Rows[this.GridView.EditIndex].FindControl("CheckBoxEditable")
if (item.IsEditable)
chbEdit.Checked = true;
new TypeService().Update(new Type
{
IsEditable = item.IsEditable,
});
this.GridView.DataBind();
}
}
catch
{
throw;
}
}
It makes sense because I am not in the right function to declare this. But I just have 3 methods in my webform.
SelectMethod="GridView_GetData"
UpdateMethod="GridView_UpdateItem"
DeleteMethod="GridView_DeleteItem"
Where can I do this?
(And I have the same problem with datas on dropdownList. I don't know where I recover current value during editing)
Thanks in advance
(Sorry I am beginner about webforms and my english is not perfect)
Evy
use the following code instead for checkbox declaration in edit template
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Convert.ToBoolean("true") %>' />
CheckBox chbx = GridView1.HeaderRow.FindControl("CheckBoxEditable") as CheckBox;
if (chbx != null && chbx.Checked)
{
//code here
}
else
{
//else condtion
}
hope this helps
Where can I do this?
Try it in RowDataBound event:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox chbEdit = (CheckBox)e.Row.FindControl("CheckBoxEditable");
string value = ((Label)e.Row.FindControl("lblID")).Text;
if (value=="True")
chbEdit.Checked = true;
else
chbEdit.Checked = false;
}
}
Note: Don't forget to add OnRowDataBound in GrindView <asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound" >
I added the property Checked=<%# BindItem.IsEditable %> on CheckBox Control and it works perfectly.
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.
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 am using VS2005 C#.
Currently I have a GridView, and I have changed one of my GridView control to my column name Gender, from the default TextBox to a DropDownList, which I gave the ID of the control to GenderList, and it contains 2 values, M and F.
I have a default update statement which is able to update the GridView after edit, which is the following:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString=
"<%$ ConnectionStrings:SODConnectionString %>" UpdateCommand="UPDATE
[UserMasterData] SET [Name] = #Name, [Age] = #Age, [ContactNo]=#ContactNo,
[Address]=#Address, [Gender]=#Gender"/>
The above UPDATE query works perfectly, and now I have changed my Gender textbox to a dropdownlist, the UPDATE query gave me an error which says:
Must declare the scalar variable "#Gender".
I assume the UPDATE query couldn't find the value from my Gender column.
I tried to modify the UPDATE query to #GenderList, but it did not work as well.
Anyone knows what I should do do the UPDATE query so that my UPDATE query can find the value from my GenderList dropdownlist in my Gender column?
Thank you.
Below is my previous Gender column with a textbox control:
<asp:BoundField HeaderText="Gender"
DataField="Gender"
SortExpression="Gender"></asp:BoundField>
Below is my Gender with the dropdownlist control:
<asp:TemplateField HeaderText="Gender" SortExpression="Gender" >
<EditItemTemplate>
<asp:DropDownList ID="GenderList" runat="server" Width="50px" >
<asp:ListItem>M</asp:ListItem>
<asp:ListItem>F</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
EDIT:
Tried implementing RowDatBound and onRowUpdating:
RowDatBound
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataRowView dRowView = (DataRowView)e.Row.DataItem;
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
DropDownList genderList= (DropDownList)e.Row.FindControl("GenderList");
genderList.SelectedValue = dRowView[2].ToString();
}
}
}
RowUpdating.aspx.cs
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
DropDownList genderSelect =(DropDownList)GridView1.Rows[e.RowIndex].FindControl("GenderList");
SqlDataSource1.UpdateParameters["Gender"].DefaultValue =
genderSelect.SelectedValue; --> error says not set to an instance of an object
}
If you are using SqlDataSource and updating data via it then all you have to do is to set 2 way binding for the dropdownlist GenderList.
You can set this via the designer or directly in source also
<EditItemTemplate>
<asp:DropDownList ID="GenderList" runat="server"
SelectedValue='<%# Bind("Gender") %>'>
<asp:ListItem>M</asp:ListItem>
<asp:ListItem>F</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
notice that here 2 way binding is being used.
you need to use its selected value property, dropdown list is collection of values, but you are assigning a scalar value to your update statements.
You have to check when its in update mode, you need to get selected item value of dropdown list and use that value as parameter for update statements.
Editing with Dropdownlist in gridview
to check its in edit mode or not use like this in RowDataBound Event
if ((e.Row.RowState & DataControlRowState.Edit) > 0)