I am trying to access the values in the drop down list from the code behind but I am getting this error: “ddl_Ext does not exist in the current context”. I am not sure what am I doing wrong here.. can someone please help? Here is my drop-down list in aspx file. Thanks.
<asp:TemplateField HeaderText="Is this external?">
<ItemTemplate>
<asp:Label ID="lblExt" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Ext") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddl_Ext" runat="server"
AutoPostBack="false" AppendDataBoundItems="true">
<asp:ListItem Text="Please select ..." Value="Please Select ..."></asp:ListItem>
<asp:ListItem Text="Yes" Value="Yes"></asp:ListItem>
<asp:ListItem Text="No" Value="No"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
here is the code behind i am trying to use:
protected void DV_WScript_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
string Ext = ddl_Ext.SelectedValue;
}
Try this:
protected void DV_WScript_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
//Put here if you want to find control of your Insert Mode
DropDownList dropDown = (DropDownList)DetailsView1.FindControl("ddl_Ext");
string Ext = dropDown.selectedValue;
}
Related
hi i have gridview with three columns Product Name,unit,rate.... product Name have combo box what i want when i select product from combo box it will select unit and rate from database and display into unit and rate column for example if i select product Rice it select unit=kg and rate=100 from database please guide me how to acheive this.
as I understand you have a GridView like below :
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Value="1">Test1</asp:ListItem>
<asp:ListItem Value="2">Test2</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
you can use your controls like this :
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Control cont = (Control)sender;
GridViewRow row = (GridViewRow)(cont).NamingContainer;
int rowID = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);
DropDownList dropdown = (DropDownList)row.Cells[0].FindControl("DropDownList1");
///now you have dropdownlist.seletedvalue
TextBox text1 = (TextBox)row.Cells[0].FindControl("TextBox1");
///now you have textbox1.text
TextBox text2 = (TextBox)row.Cells[0].FindControl("TextBox2");
///now you have textbox2.text
}
I've started creating an online quiz. I am using a DetailsView linked to a sqlDataSource.
Here's my code:
<asp:DetailsView ID="QuestionDetails" runat="server" AutoGenerateRows="False" DataKeyNames="QuestionID,QuizID" DataSourceID="SqlDataSource_Quiz">
<Fields>
<asp:TemplateField HeaderText="Text_Eng" SortExpression="Text_Eng" ShowHeader="False">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Text_Eng") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Text_Eng") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Text_Eng") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:RadioButton ID="Option1" AccessKey="1" runat="server" Text='<%# Bind("Opt_1") %>' GroupName="AnswerOptions" AutoPostBack="true" OnCheckedChanged="Option_CheckedChanged" />
<br />
<asp:RadioButton ID="Option2" AccessKey="2" runat="server" Text='<%# Bind("Opt_2") %>' GroupName="AnswerOptions" AutoPostBack="true" OnCheckedChanged="Option_CheckedChanged"/>
<br />
<asp:RadioButton ID="Option3" AccessKey="3" runat="server" Text='<%# Bind("Opt_3") %>' GroupName="AnswerOptions" AutoPostBack="true" OnCheckedChanged="Option_CheckedChanged"/>
<br />
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
Note that i am not displaying all data retrieved from the sqlDataSource (there are other fields/columns as well) in the DetailsView.
Now, in my code behind, i am showing the next record by changing the PageIndex of the DetailsView upon clicking a button (NextButton) but at the same time, i need to retrieve some data of the currently displayed record so I've put the following in the button's click handler:
protected void NextButton_Click(object sender, EventArgs e)
{
try
{
// Save off previous answers
DataRowView dr = (DataRowView)QuestionDetails.DataItem;
// Create Answer object to save values
Answer a = new Answer();
a.QuestionID = dr["QuestionID"].ToString();
a.CorrectAnswer = dr["CorrectAnswer"].ToString();
a.UserAnswer = selectedOptionRB.AccessKey.ToString();
ArrayList al = (ArrayList)Session["userAnswers"];
al.Add(a);
Session.Add("userAnswers", al);
}
catch (Exception ex)
{
Label2.Text = "Exception!";
}
if (QuestionDetails.PageIndex == QuestionDetails.PageCount - 1)
{
NextButton.Enabled = false;
}
else
{
QuestionDetails.PageIndex += 1;
NextButton.Enabled = false;
}
if (QuestionDetails.PageIndex == QuestionDetails.PageCount - 1)
{
NextButton.Text = "Finish the Quiz";
}
}
So when i run the code, i get "Exception!" displayed in my Label2 which i setup for testing.
What am i doing wrong?
C# code:
protected void DropDownListDB_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownListDB.SelectedValue == "Other")
{
LabelIfOtherDb.Visible = true;
TextBoxIfOtherDb.Visible = true;
}
}
ASP code:
<asp:DropDownList AutoPostBack="True" ID="DropDownListDB" runat="server" Height="20px"
Width="158px">
<asp:ListItem>- Select -</asp:ListItem>
<asp:ListItem>Oracle</asp:ListItem>
<asp:ListItem>MS SQL Server</asp:ListItem>
<asp:ListItem>MySQL</asp:ListItem>
<asp:ListItem>MS Access</asp:ListItem>
<asp:ListItem>Other</asp:ListItem>
</asp:DropDownList>
I have AutoPostBack="True" but still it doesn't show my hidden textbox/label.. Any suggestions?
It seems, your Event is not connected to the event-handler.
Two possibilities: either, define the event-handler in the Markup, like:
<asp:DropDownList AutoPostBack="True" ID="DropDownListDB" runat="server" Height="20px" SelectedIndexChanged="DropDownListDB_SelectedIndexChanged">
or in the code-behind
DropDownListDB.SelectedIndexChanged += DrowpDownListDB_SelectedIndexChanged;
You should put this up in mark-up though.
This works have the very same question for a college assignment
<asp:DropDownList ID="DropDownList1" AutoPostBack="true"
runat="server" Height="16px" Width="237px"
onselectedindexchanged="DropDownList1_SelectedIndexChanged"/>
here is my code
aspx
<telerik:GridEditCommandColumn HeaderStyle-Width="25px" EditImageUrl="../images/Edit.gif"
ButtonType="ImageButton" ItemStyle-HorizontalAlign="Right">
</telerik:GridEditCommandColumn>
<telerik:GridTemplateColumn HeaderText="Comments" HeaderStyle-Width="400px" DataField="Comments"
HeaderStyle-CssClass="tblHeaderNoBorder">
<ItemTemplate>
<asp:Label runat="server" ID="lblComments" Text='<%# Eval("Comments") %>' />
</ItemTemplate>
<EditItemTemplate>
<telerik:RadTextBox ID="txtComments" runat="server" Height="40px" Width="100%" TextMode="MultiLine"
Enabled="true" Text='<%# Eval("Comments") %>' BackColor="LightPink">
</telerik:RadTextBox></EditItemTemplate>
</telerik:GridTemplateColumn>
aspx.cs
protected void RadGrid1_UpdateCommand(object source, GridCommandEventArgs e)
{
if ((e.Item is GridEditableItem) && (e.Item.IsInEditMode))
{
GridEditableItem item = (GridEditableItem)e.Item;
RadTextBox txtComments = (RadTextBox)item.FindControl("txtComments");
RadGrid1.DataBind();
}
here's the scenario, when i click on the update this must update the record.
how can i update the record? please help me.
I also set AllowAutomaticUpdate = "true"
Check if you have put update command or not.
You must have to create update command of the datasource related to radgrid otherwise it can't update.
otherwise you have to put update logic in
protected void RadGrid1_UpdateCommand
I am doing insert/update and delete in gridview. For that I am using ItemTemplate which contains labels to show the values. But when the gridview is in edit mode, the dropdown lists comes in place of that labels. I want to set the selected values of drop down lists to the values of labels. My drop down lists dont have datasource. I am binding dropdown list from 0 to 99. Below is the code for my edit method.
protected void grdUsedCatheters_RowEditing(object sender, GridViewEditEventArgs e)
{
try
{
grdUsedCatheters.EditIndex = e.NewEditIndex;
BindCatheterGrid();
DropDownList ddlFrom = (DropDownList)grdUsedCatheters.Rows[e.NewEditIndex].FindControl("ddFrom");
DropDownList ddlTo = (DropDownList)grdUsedCatheters.Rows[e.NewEditIndex].FindControl("ddTo");
BindDropDowns(ddlFrom);
BindDropDowns(ddlTo);
}
catch (Exception ex)
{
if (ex.HelpLink == null)
lblMessage.Text = ex.Message;
else
lblMessage.Text = ex.HelpLink;
lblMessage.CssClass = "ERROR";
}
private void BindDropDowns(DropDownList ddl)
{
for (int i = 0; i <= 99; i++)
ddl.Items.Add(i.ToString());
}
below is the part of markup of my gridview
<asp:TemplateField HeaderText="Cine Run">
<ItemTemplate>
From: <asp:Label ID="lblFrom" runat="server" ><%# Eval("CineRunFrom")%></asp:Label>
To: <asp:Label ID="lblTo" runat="server"><%# Eval("CineRunTo")%></asp:Label>
</ItemTemplate>
<EditItemTemplate>
From: <asp:DropDownList ID="ddFrom" runat="server" Width="50px">
</asp:DropDownList>
To: <asp:DropDownList ID="ddTo" runat="server" Width="50px">
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
From: <asp:DropDownList ID="ddFromF" runat="server" Width="50px"> </asp:DropDownList>
To: <asp:DropDownList ID="ddToF" runat="server" Width="50px"> </asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
}
Retrieve the values of label's before setting grdUsedCatheters.EditIndex = e.NewEditIndex and calling BindCatheterGrid() method and then after populating the DropDownLists set their selected value accordingly. Like this:
protected void grdUsedCatheters_RowEditing(object sender, GridViewEditEventArgs e)
{
try
{
Label lblFrom = (Label)grdUsedCatheters.Rows[e.NewEditIndex].FindControl("lblFrom"); //lblFrom is the ID of label
grdUsedCatheters.EditIndex = e.NewEditIndex;
BindCatheterGrid();
DropDownList ddlFrom = (DropDownList)grdUsedCatheters.Rows[e.NewEditIndex].FindControl("ddFrom");
DropDownList ddlTo = (DropDownList)grdUsedCatheters.Rows[e.NewEditIndex].FindControl("ddTo");
BindDropDowns(ddlFrom);
BindDropDowns(ddlTo);
ddlFrom.Text = lblFrom.Text;
}
catch (Exception ex)
{
if (ex.HelpLink == null)
lblMessage.Text = ex.Message;
else
lblMessage.Text = ex.HelpLink;
lblMessage.CssClass = "ERROR";
}
}
Edit
and also change your gridview markup like this:
<asp:TemplateField HeaderText="Cine Run">
<ItemTemplate>
From: <asp:Label ID="lblFrom" runat="server" Text='<%# Eval("CineRunFrom")%>' />
To: <asp:Label ID="lblTo" runat="server" Text='<%# Eval("CineRunTo")%>' />
</ItemTemplate>
...
I think this example will work for you.
First you put hidden field in EditItemTemplate where u have put the Dropdownlist.
Set the value of hidden field as you set the value of label in ItemTemplate
See my code:
<asp:GridView runat="server" ID="gridExample" OnRowEditing="gridExample_RowEditing"
AutoGenerateEditButton="True" AutoGenerateColumns ="false" OnRowCancelingEdit ="gridExample_RowCancelingEdit" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label runat="server" ID="lblID" Text='<%# Eval("ID") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList runat="server" ID="drpName">
</asp:DropDownList>
<asp:HiddenField runat ="server" ID ="hdnId" Value ='<%# Eval("ID") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label runat="server" ID="lblName" Text='<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate >
<asp:TextBox runat ="server" ID="txtName" Text ='<%# Eval("Name") %>' ></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void gridExample_RowEditing(object sender, GridViewEditEventArgs e)
{
gridExample.EditIndex = e.NewEditIndex;
BindGrid();
DropDownList dl=new DropDownList ();
dl = (DropDownList)gridExample.Rows[gridExample.EditIndex].FindControl("drpName");
FillDrops(dl);
HiddenField hdnId = new HiddenField();
hdnId = (HiddenField)gridExample.Rows[gridExample.EditIndex].FindControl("hdnId");
dl.Text = hdnId.Value;
}