i am trying to make a "clock in / out site" and one of the functions is for the user to edit old clock ins/outs. i have added a dropdown with 2 values "IND" and "UD". How do i get these values out, and send them to my "CHECKIN" sql value. I hope you know what iam trying to explain.
this is what it looks like:
The Dropdown:
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server"
OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged">
<asp:ListItem Selected="True">Ind</asp:ListItem>
<asp:ListItem Value="UD">Ud</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
what ive tried:
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = GridView1.SelectedRow;
string val = row.Cells[3].ToString();
SqlDataSource1.SelectParameters.Add("CHECKEDIN", val);
}
and aswell as:
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)GridView1.FindControl("DropDownList2");
string val = ddl.SelectedValue;
SqlDataSource1.SelectParameters.Add("CHECKEDIN", val);
}
Both of these return "null" tho
Use RowDataBound event of GridView.
Code
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow
&& GridView1.EditIndex == e.Row.RowIndex)
{
DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList2");
string val = ddl.SelectedValue.ToString();
SqlDataSource1.SelectParameters.Add("CHECKEDIN", val);
}
}
You can cast the sender back to a DropDownlist and get the selected value
DropDownList dropDownList = sender as DropDownList;
string val = dropDownList.SelectedValue;
But how will you know with this method which row the DropDownList belongs to?
You can use a trick for that, like abusing the CSS class to get the correct row number.
<asp:DropDownList ID="DropDownList2" runat="server" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged" AutoPostBack="true" CssClass='<%# Container.DataItemIndex %>'>
And get the row number in code behind.
int rowNumber = Convert.ToInt32(dropDownList.CssClass);
Related
I'd like the DropDownList to be disabled and enable it only after I click on the edit link on Gridview. As of now, it is showing the DropDownList to be disabled before and after edit link.
codes:
<asp:DropDownList ID="DropDownList1" runat="server" Height="30px" Width="190px" SelectedValue='<%# Eval("FAQGroup") %>' Enabled="false" >
<asp:ListItem Value="Most asked FAQ"></asp:ListItem>
<asp:ListItem Value="Normal FAQ"></asp:ListItem>
</asp:DropDownList>
aspx.cs
protected void gvFAQ_RowEditing(object sender, GridViewEditEventArgs e)
{
gvFAQ.Columns[3].Visible = true;
DropDownList DDL= (DropDownList)gvFAQ.Rows[e.NewEditIndex].FindControl("DropDownList1");
DDL.Enabled = true;
gvFAQ.EditIndex = e.NewEditIndex;
bind();
}
When you call bind at the end of the RowEditing event handler, the GridView is cleared and refilled, and a new DropDownList is created in each row. The control must be enabled after the data is bound, for example in the RowDataBound event handler:
protected void gvFAQ_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = e.Row.FindControl("DropDownList1") as DropDownList;
ddl.Enabled = e.Row.RowIndex == gvFAQ.EditIndex;
}
}
In my application in c#, when I edit a row in the gridview I choose some new data from a dropdownlist.
I am populating the dropdown like this:
<asp:TemplateField HeaderText="Gender">
<ItemTemplate>
<asp:Label ID="gender" runat="server" Text='<%# Eval("gender").ToString() %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DDL_genderList" runat="server">
<asp:ListItem Value="" Text="---"></asp:ListItem>
<asp:ListItem Value="M" Text="M"> </asp:ListItem>
<asp:ListItem Value="F" Text="F"> </asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
But when I press the 'Edit' button from the template and enters in the 'RowUpdating' event, the selected value from the dropdownlist is every time the first value from that dropdownlist.
I need selected value in dropdownlist it's the value which displays in Label gender.
Does anyone have any ideas?
I've tried many ways to set the selected value in the 'RowDataBound' event, but with no luck.
I tried this:
protected void gvProducts_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataRowView dRowView = (DataRowView)e.Row.DataItem;
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
DropDownList genderList = (DropDownList)e.Row.FindControl("DDL_genderList");
genderList.SelectedValue = dRowView[1].ToString();
}
}
You have to find Label from TemplateField (DataRow) and DropDownList from EditTemplate (EditRowState) as like:
protected void gvProducts_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// find gender from label which is inside ItemTemplate
string lblgender = ((Label)e.Row.FindControl("gender")).Text;
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
// find gender list from dropdownlist which is inside EditTemplate
DropDownList genderList = (DropDownList)e.Row.FindControl("DDL_genderList");
genderList.SelectedIndex =
genderList.Items.IndexOf(genderList.Items.FindByValue(lblgender));
}
}
}
I know this is very late answer but it can help to the OP.
I have a GridView that I have placed in DropDownList's in 2 columns.
<asp:TemplateField HeaderText="Upgrade" SortExpression="Upgrade">
<ItemTemplate>
<asp:Label ID="LabelUpgrade" runat="server" Text='<%# Eval("Upgrade") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlUpgrade" runat="server" Width="100px">
<asp:ListItem Value="1">--Select--</asp:ListItem>
<asp:ListItem Value="2">1</asp:ListItem>
<asp:ListItem Value="3">2</asp:ListItem>
<asp:ListItem Value="4">3</asp:ListItem>
<asp:ListItem Value="5">4</asp:ListItem>
<asp:ListItem Value="6">5</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
how do I grab the item from ddlUpgrade in the codebehind?
OnUpdating Event - I don't have a way to pull the row to get the value from the drop down but I add my sql parameters here.
protected void IAP_Updating(object sender, SqlDataSourceCommandEventArgs e){}
RowUpdating Event - I can get the row here but I can't add the value to the sql parameters because e.command isn't valid here
protected void gvClients_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow _row = gvClients.Rows[e.RowIndex];
DropDownList _ddl = (DropDownList)_row.FindControl("ddlUpgrade");
SqlParameter _parm = new SqlParameter("#Upgrade", _ddl.SelectedItem.ToString());
}
On the RowUpdating event you can capture the control inside the edit template based on its ID.
GridViewRow row = GridView1.Rows[e.RowIndex];
DropDownList ddl = (DropDownList)row.FindControl("ddlUpgrade");
SqlParameter _parm = new SqlParameter("#Upgrade", ddl.SelectedItem.ToString());
e.Command.Parameters.Add(_parm);
I would add a hidden field outside the GridView:
<asp:HiddenField ID="hdnSelection" value="" runat="server" />
And change the gvClients_RowUpdating method:
protected void gvClients_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow _row = gvClients.Rows[e.RowIndex];
DropDownList _ddl = _row.FindControl("ddlUpgrade") as DropDownList;
if(_ddl != null)
{
hdnSelection.Value = _ddl.SelectedItem.Text;
IAP.Update();//Assuming IAP is the ID of the SqlDataSource
}
}
And my IAP_Updating method should look like this:
protected void IAP_Updating(object sender, SqlDataSourceCommandEventArgs e)
{
SqlParameter _parm = new SqlParameter("#Upgrade", hdnSelection.Value);
e.Command.Parameters.Add(_parm);
}
I did not test the code. You may need to tweak.
I have a gridview which is being loaded from database table.
There is one column in the table named 'valid',whose values are 'Y' or 'N'.
Now, what I want to do is, to check if the value of 'valid' is Y, then the whole record in that row should appear as disabled when loading the gridview.
How to proceed with that? should I write a procedure for this or it can be done from code behind?
Note: the column Valid is not a part of gridview.
You need to use RowDataBound event of gridview to disable rows.
Note that if you use databound columns then it is rendered in tr and td then you need to find the cell and then set the disable property.
protected void gvEntity_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//CheckBox cbAttachClrReq = (CheckBox) e.Row.FindControl("chkAdd");
//check the value here and set enable property
e.Row.Enabled = false;
}
}
As #Hanlet EscaƱo has suggested you can add the field value in the same column as follows
<asp:TemplateField>
<ItemTemplate>
<<asp:Label ID="lbl1" runat="server"
Value='<%# Eval("Name") %>' />
<asp:HiddenField ID="HiddenField1" runat="server"
Value='<%# Eval("valid") %>' />
</ItemTemplate>
and get the hidden field value in row-databound event as follows
protected void gvEntity_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
HiddenField Hf = (HiddenField) e.Row.FindControl("HiddenField1");
if(Hf.Value=="Y")
e.Row.Enabled = false;
}
}
Try the following.
This example gets the DataRowView per record and then extracts the valid column from the underlying datasource to test against.
protected void grdDetails_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var dr = e.Row.DataItem as DataRowView;
if(Convert.ToString(dr["valid"]) == 'Y')
e.Row.Enabled = false;
}
}
and the markup
<asp:GridView ID="grdDetails" runat="server" OnRowDataBound="grdDetails_RowDataBound"
In my application, when I edit a row in the gridview I choose some new data from a dropdownlist.
I am populating the dropdown like this:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
DropDownList emailsListDL = (DropDownList)e.Row.FindControl("emailsDL");
emailsListDL.DataSource = allUsersEmails;//list of strings with the emails of the users
emailsListDL.DataBind();
}
}
}
But when I press the 'Update' button from the template and enters in the 'RowUpdating' event, the selected value from the dropdownlist is every time the first value from that dropdownlist.
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
DropDownList emailsListDL = (DropDownList)GridViewAdvertisers.Rows[e.RowIndex].FindControl("emailsDL");
string email = emailsListDL.SelectedValue; //the selected value is every time the first value from the dropdownlist
}
Does anyone have any ideas?
I've tried many ways to set the selected value in the 'RowDataBound' event, but with no luck. I tried this:
1. emailsListDL.SelectedIndex = emailsListDL.Items.IndexOf(emailsListDL.Items.FindByValue(DataBinder.Eval(e.Row.DataItem, "OwnerMail").ToString()));
2. emailsListDL.SelectedValue = GridViewAdvertisers.DataKeys[e.Row.RowIndex]["OwnerMail"].ToString();
3. emailsListDL.SelectedValue = GridViewAdvertisers.Rows[e.Row.RowIndex].Cells[1].Text;
//ownerMail is a string (object) from the list of objects that I put as datasource to the gridview
Thanks,
Jeff
Update
My Item template from the aspx page is:
<asp:TemplateField ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle"
ItemStyle-Width="150px" HeaderText="Owner Email" HeaderStyle-HorizontalAlign="Left"
HeaderStyle-BorderWidth="1px" HeaderStyle-BorderColor="#e1e1e1">
<ItemTemplate>
<asp:Label ID="LabelEmail" runat="server" Text='<%# Bind("OwnerMail")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="emailsDL" runat="server" Width="150">
</asp:DropDownList>
</EditItemTemplate>
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Font-Bold="True"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="180px" BorderWidth="1px"
BorderColor="#e1e1e1"></ItemStyle>
</asp:TemplateField>
The SelectedIndex will always default to 0 if you don't define it in your DropDownList definition.
Edit: #Tim Schmelter should add his comment as an anwer. In the meantime, I'll paraphrase for other readers: You need to check for postback (see comments above).
You can declare a ComboBox mainBX = new Combobox();
and you can declare an event
private void onSeletedIndexChange(object sender,EventArgs e)
{
mainBX = (ComboBox)(sender);
}
and next you should iterate foreach ComboBox in GridView and Add this Event .
Than all you should do is ,take the mainBX.seletedItem();
I think this is what you need ,if not apologise .
To set the Selected Value in the RowDataBound-Event, you should do it like this:
(DropDownList)e.Row.Cells[/*index*/].Controls[/*index, or use FindControl*/]).Items.FindByValue(((DataRowView)e.Row.DataItem).Row.ItemArray[/*columnnumber*/].ToString()).Selected = true;
FindByValue finds the current Textvalue of the Field in the "normal" Viewmode and sets the DropDownList with the value.
Of course this needs to be encapsulated in
if ((e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit)
{
}
As for your "getting the Value at Update"-Problem, I must honestly say I've got no clue, since your approach is exactly like mine, only difference: mine works.
Here's my code if it's any help:
protected void gridVariables_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string control = ((DropDownList)gridVariables.Rows[e.RowIndex].Cells[3].Controls[1]).SelectedValue;
gridVariables.EditIndex = -1;
this.gridVariables_DataBind(control, e.RowIndex);
}
private void gridVariables_DataBind(string control, int index)
{
DataTable dt = (DataTable)Session["variableTable"]; //features a DataTable with the Contents of the Gridview
dt.Rows[index]["ControlType"] = control;
gridVariables.DataSource = dt;
gridVariables.DataBind();
}
I had the same problem with a different solution, I had implemented custom paging on the GridView with a custom pager, and in the process added the RowCommand method, which was rebinding the grid with a new page index. As it happens though the RowCommand method is also called during Updating.
So be sure to check any other locations you may be binding anywhere in your code.
Hope this helps somebody else.
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
string commandName = e.CommandName;
switch (commandName)
{
case "Page1":
HandlePageCommand(e);
//binding should happen here
break;
}
//this line is in the wrong location, causing the bug
BindGrid1();
}