Unable to set selected item from dropdownlist in formview - c#

The problem is I am unable to set a dropdownlist selected item. I have a panel with two dropdownlists. When you select a product group from the first dropdown (editMerchGroupDropDownList1), it should set the selected value of the second dropdown (MerchGroupDropDownList) according the ProductGroup's CategoryID value.
Category: ID, Name
ProductGroup: ID, Name, CategoryID
Even when I try a fixed value, it wont change the selected item.
MerchGroupDropDownList.Items.FindByValue("2").Selected = true;
ASP.NET code:
<asp:Panel ID="editMerchGroup" runat="server">
<h3>Edit Mechandise Group:</h3>
Select a Mechandise Group to edit:
<asp:DropDownList ID="editMerchGroupDropDownList1" runat="server"
ItemType="App.Models.ProductGroup"
SelectMethod="GetMerchProductGroups" DataTextField="ProductGroupName"
DataValueField="ProductGroupID" OnSelectedIndexChanged="editMerchGroupDropDownList1_SelectedIndexChanged" AutoPostBack="true">
</asp:DropDownList>
<asp:FormView ID="editMerchGroupFormView1" runat="server" ItemType="App.Models.ProductGroup" SelectMethod="GetMerchGroup" RenderOuterTable="false">
<ItemTemplate>
<asp:HiddenField ID="editMerchGroupHiddenField" runat="server" value="<%#:Item.CategoryID %>" />
<br />
<br />
<table>
<tr>
<td>
<asp:Label ID="Label4" runat="server">Belongs To Category:</asp:Label></td>
<td>
<asp:DropDownList ID="MerchGroupDropDownList" runat="server"
ItemType="App.Models.Category"
SelectMethod="GetMerchCategories" DataTextField="CategoryName"
DataValueField="CategoryID" >
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
<asp:Label ID="MerchGroupNameLabel" runat="server">Name:</asp:Label>
</td>
<td>
<asp:TextBox ID="editMerchGroupNameTextBox" Text="" runat="server" ></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator6" ValidationGroup="EditMerchGroup" runat="server" Text="* Merchandise name required."
ControlToValidate="editMerchGroupNameTextBox" SetFocusOnError="true" Display="Dynamic"></asp:RequiredFieldValidator>
</td>
</tr>
</table>
<asp:Button ID="editMerchGroupButton" runat="server" Text="Update Merchandise Group" OnClick="editMerchGroupButton_Click" ValidationGroup="EditMerchGroup" CausesValidation="true" />
<br />
</ItemTemplate>
<EmptyDataTemplate><b>No content found.</b></EmptyDataTemplate>
</asp:FormView>
</asp:Panel>
CS file:
protected void editMerchGroupDropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
editMerchGroupFormView1.DataBind();
HiddenField myHiddenField = (HiddenField)FindControlRecursive(Page, "editMerchGroupHiddenField");
MerchGroupDropDownList.DataBind();
MerchGroupDropDownList.Items.FindByValue(myHiddenField.Value).Selected = true;
}

Related

Why data is not displayed on my asp.net webpage?

I am developing Canteen Management System where I am displaying menuList from Database using gridview like given below.. ( this is Menu.aspx page)
<asp:DataList ID="DataList1" runat="server" RepeatColumns="3" RepeatDirection="Horizontal" OnItemCommand="DataList1_ItemCommand">
<ItemTemplate>
<table class="nav-justified" style="height: 111px">
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("menuName") %>'></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:ImageButton ID="ImageButton1" runat="server" CommandName="viewDetail" CommandArgument='<%# Eval("Id") %>' ImageUrl='<%# Eval("menuImage") %>' />
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("menuPrice") %>'></asp:Label>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
<br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Cms_AspFormsConnectionString %>" SelectCommand="SELECT [menuName], [menuPrice], [menuImage], [Id] FROM [menuInfo]"></asp:SqlDataSource>
This is Menu.aspx.cs page
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if ( e.CommandName == "viewDetail" )
{
Response.Redirect("MenuDetails.aspx?Id=" + e.CommandArgument.ToString());
}
}
but When i run this this it's showing nothing, ( when i go to source page it was all empty at like given below..)
<div>
<br />
</div>
Why it' showing empty? and how to resolve this?
Add DataSourceID="SqlDataSource1" to the DataList tag. Data source was not set for the control.

Radiobutton in modalpopup causing postback even when inside UpdatePanel

Posted is an abridged code of my user control. I have added an update panel over radio button and want to execute a server function which will toggle between consultant drop down list and reference name text box based on the selection without a full post back. But the radio button is causing a full postback. What am I doing wrong here?
<aj:ModalPopupExtender ID="ModalPopup1" runat="server" PopupControlID="Panel1"
BackgroundCssClass="PopupBackground" CancelControlID="closeButton">
</aj:ModalPopupExtender>
<asp:Panel ID="Panel1" runat="server" CssClass="PopupPanel" align="center" style = "display:none">
<h1 class="PopupHeading">Add New Candidate</h1>
<table class="PopupTable">
<tr>
<td>
<asp:Label ID="firstNameLabel" runat="server" Text="First Name" CssClass="PopupLabel"></asp:Label>
</td>
<td>
<asp:TextBox ID="firstNameTextBox" runat="server" Font-Size="14px" ></asp:TextBox>
</td>
<td class="ValidationError">
<asp:RequiredFieldValidator ID="nameRequiredFieldValidator" runat="server"
ValidationGroup="AddCandidateValidationGroup"
ErrorMessage="Name is required" Text="*"
ControlToValidate="firstNameTextBox" CssClass="failureNotification">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:Label ID="emailLabel" runat="server" Text="Email" CssClass="PopupLabel"></asp:Label>
</td>
<td>
<asp:TextBox ID="emailTextBox" runat="server" Font-Size="14px" ></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ValidationGroup="AddCandidateValidationGroup"
ErrorMessage="Email is required" Text="*"
ControlToValidate="emailTextBox" CssClass="failureNotification">*</asp:RequiredFieldValidator>
</td>
<td>
<asp:RegularExpressionValidator ID="emailRegularExpressionValidator"
runat="server" ErrorMessage="Incorrect Email"
ValidationGroup="AddCandidateValidationGroup"
ControlToValidate="emailTextBox"
ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*"
CssClass="failureNotification" Text="Incorrect email">
</asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td>
<asp:Label ID="referredByLabel" runat="server" Text="Referred By" CssClass="PopupLabel"></asp:Label>
</td>
<td>
<asp:UpdatePanel ID="updatepanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:RadioButtonList ID="RefConsRadButList" runat="server" AutoPostBack="true"
RepeatDirection="Horizontal" OnSelectedIndexChanged="RefConsRadButList_SelectedIndexChanged" CausesValidation="false">
<asp:ListItem >Consultant</asp:ListItem>
<asp:ListItem>Referral</asp:ListItem>
<asp:ListItem Selected="True">N/A</asp:ListItem>
</asp:RadioButtonList>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="RefConsRadButList" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
</td>
</tr>
<tr>
<td>
<asp:Label ID="consultantNameLabel" runat="server" Text="Consultant Name" Visible="false" CssClass="PopupLabel"></asp:Label>
</td>
<td>
<asp:DropDownList ID="ConsultantDrDoList" runat="server" Visible="false">
<asp:ListItem>Select Consultant</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
<asp:Label ID="referenceNameLabel" runat="server" Text="Reference Name" Visible = "false" CssClass="PopupLabel"></asp:Label>
</td>
<td>
<asp:TextBox ID="ReferralNameTextBox" runat="server" Visible="false"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="refNameRequiredFieldValidator" runat="server"
ValidationGroup="AddCandidateValidationGroup"
ErrorMessage="Referral name is required" Text="*"
ControlToValidate="phoneTextBox" CssClass="failureNotification">*</asp:RequiredFieldValidator>
</td>
</tr>
</table>
<br />
<asp:Button ID="addButton" runat="server" Text="Add" onclick="addButton_Click" ValidationGroup="AddCandidateValidationGroup"/>
<asp:Button ID="closeButton" runat="server" Text="Close" />
<div id="AddStatus">
<asp:ValidationSummary ID="ValidationSummary1" runat="server"
ValidationGroup="AddCandidateValidationGroup"
HeaderText="Add user failed due to the following errors:" CssClass="PopupValidationError"/>
</div>
</asp:Panel>
instead of using updatepanel on modalpopup panel you can try something from code behind to serve the purpose.
<asp:scriptmanager id="ScriptManager1" runat="server">
</asp:scriptmanager>
<asp:ModalPopupExtender ID="ModalPopupExtender1" runat="server" BackgroundCssClass="modalBackground"
CancelControlID="btnCancel" PopupControlID="Panel1"
TargetControlID="HiddenField1">
</asp:ModalPopupExtender>
<asp:Panel ID="Panel1" runat="server" CssClass="Panel">
<asp:RadioButtonList ID="RadioButtonList1" runat="server" Height="34px" RepeatDirection="Horizontal"
Width="129px" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged"
AutoPostBack="True">
<asp:ListItem>Option1</asp:ListItem>
<asp:ListItem>Option2</asp:ListItem>
</asp:RadioButtonList>
<asp:TextBox ID="txt1" runat="server"
Visible="False"></asp:TextBox>
<asp:TextBox ID="txt2" runat="server"
Visible="False"></asp:TextBox>
</asp:Panel>
===========================================
Code Behind:
===========================================
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (RadioButtonList1.SelectedIndex == 0)
{
txt1.Visible = true;
ModalPopupExtender1.Show();
}
else
{
txt2.Visible = true;
ModalPopupExtender1.Show();
}
}

How to databind to a Gridview where column values should appear in sub rows within a main row?

Please can anyone suggest a way to databind a database table to a gridview but in a specific layout where column values should appear in sub rows within a main row, like wise all rows from the database table should list down inside that Gridview.I know the normal way of databinding to a gridview Where it display row data(From Database Table) as columns, but what I want is like below
Gridview is not suitable for this type of format.
If you are okay with repeater, you can do something like this:
And here's a link to MSDN: Repeater Class.
UPDATE: If you want to post each answer selection, you can use option button and group them. You can use questionid as a part of group name and in the code get the question id. Your markup may look like below:
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table border="1">
<tr>
<th colspan="2"></th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%# DataBinder.Eval(Container.DataItem, "QuestionId") %> </td>
<td>
<table>
<tr>
<td colspan="4"><%# DataBinder.Eval(Container.DataItem, "Question") %> </td>
</tr>
<tr>
<td>
<asp:RadioButton ID="RadioButton1" runat="server" AutoPostBack="true" Text='<%# DataBinder.Eval(Container.DataItem, "Answer1") %>'
OnCheckedChanged="RadioButton1_CheckedChanged" GroupName='<%# Eval("QuestionId","Grp_{0}") %>' TextAlign="Left" />
</td>
<td><asp:RadioButton ID="RadioButton2" runat="server" AutoPostBack="true" Text='<%# DataBinder.Eval(Container.DataItem, "Answer2") %>'
OnCheckedChanged="RadioButton2_CheckedChanged" GroupName='<%# Eval("QuestionId","Grp_{0}") %>' TextAlign="Left" />
</td>
<td><asp:RadioButton ID="RadioButton3" runat="server" AutoPostBack="true" Text='<%# DataBinder.Eval(Container.DataItem, "Answer3") %>'
OnCheckedChanged="RadioButton3_CheckedChanged" GroupName='<%# Eval("QuestionId","Grp_{0}") %>' TextAlign="Left" />
</td>
<td><asp:RadioButton ID="RadioButton4" runat="server" AutoPostBack="true" Text='<%# DataBinder.Eval(Container.DataItem, "Answer4") %>'
OnCheckedChanged="RadioButton4_CheckedChanged" GroupName='<%# Eval("QuestionId","Grp_{0}") %>' TextAlign="Left" />
</td>
</tr>
</table>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
And in the code:
protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
string grpId = ((RadioButton)sender).GroupName;
int questionId = 0;
int.TryParse(grpId.Split('_')[1].ToString(), out questionId);
//Use questionId
}

C#,asp.net gridview ,edit a value using the radiobutton

I am using a grid in which inside the item template(the first one in which radio button is there). i use link button instead of radiobutton ,when i click on it iam able to edit as the textbox appears, but if i use the radio button,that editing option is not coming;
aspx:
<div id="loginblock">
<table>
<tr>
<td>UserName:</td>
<td>
<asp:TextBox ID="username" runat="server" onBlur="txtvalidation();" AutoPostBack="true"></asp:TextBox>
</td>
<td>
<div id="warning" style="color:aqua;width:100px" runat="server">
</div>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="PlS Enter Password" ControlToValidate="pwd"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>Password:</td>
<td>
<asp:TextBox ID="pwd" runat="server" TextMode="Password" ></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnLogin" runat="server" OnClick="btnLogin_Click" Text="Button" />
</td>
</tr>
<tr>
<asp:GridView ID="grd" runat="server" OnRowEditing="selectrd_CheckedChanged" AutoGenerateColumns="false" DataKeyNames="userid">
<Columns>
<asp:TemplateField HeaderText="edition">
<ItemTemplate>
<asp:RadioButton ID="rdnselect" runat="server" CommndName="edit" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="ids" DataField="userid" />
<asp:TemplateField HeaderText="password">
<ItemTemplate>
<%# Eval("pwd") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="edits" runat="server" Text='<%# Eval("pwd") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
cs:
protected void selectrd_CheckedChanged(object sender, GridViewEditEventArgs e)
{
grd.SelectedIndex = e.NewEditIndex;
ldgrid();
}
Here if we remove the radiobutton and insert link button,onclick of the link button we can edit the values,pls say me how to do the same operation with the radio button.

How to run SelectCommand in <asp:SqlDataSource> tag

I am making QueryString parameter ,so I have two .aspx forms. One form Default.aspx contains three fields SId, FirstName, LastName. when I submit the values, those values are saved in database.Now I want show this data on grid for particular SId on default2.aspx. Grid takes data through the SqlDataSource.SO I want to call the SqlDataSource on button click and grid should show data for particular SId on default2.aspx.
Default.aspx :
<body>
<form id="form1" runat="server">
<div>
</div>
<table cellpadding="2" cellspacing="5">
<tr>
<td>
<asp:Label ID="lblId" runat="server" Text="SId"></asp:Label>
</td>
<td>
<asp:TextBox ID="tbid" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblname" runat="server" Text="FirstName"></asp:Label>
</td>
<td>
<asp:TextBox ID="tbfirstname" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lbllastname" runat="server" Text="LastName"></asp:Label>
</td>
<td>
<asp:TextBox ID="tblastname" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnsubmit" runat="server" Text="Submit"
onclick="btnsubmit_Click1" Width="102px" />
</td>
</tr>
</table>
<br />
</form>
</body>
Default2.aspx :
<body>
<form id="form1" runat="server">
<div>
<table cellpadding="2" cellspacing="5">
<tr>
<td>
SId</td>
<td>
<asp:TextBox ID="tbId" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnSearch" runat="server" Text="Search"
onclick="btnSearch_Click" />
</td>
</tr>
</table>
</div>
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
AutoGenerateSelectButton="True"
DataSourceID="SqlDataSource1" EnableModelValidation="True"
onrowcommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField HeaderText="SId">
<ItemTemplate>
<asp:Label ID="lblSId" runat="server" Text='<%# Bind("SId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="FirstName">
<ItemTemplate>
<asp:Label ID="lblFirstName" runat="server" Text='<%# Bind("FirstName")%>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="LastName">
<ItemTemplate>
<asp:Label ID="lblLastName" runat="server" Text='<%# Bind("LastName")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommand="SELECT * FROM [Student] where SId = #tbId">
<SelectParameters>
<asp:QueryStringParameter Name="SId" QueryStringField="SId" />
</SelectParameters>
</asp:SqlDataSource>
</form>
</body>
I want to call the SqlDataSource1 on button click like..
Default2.aspx.cs :
protected void btnSearch_Click(object sender, EventArgs e)
{
string SId = tbId.Text;
SqlDataSource1.DataBind();
}
You don't need the button click. The SqlDataSource executes on every post back. The search button is going to issue a post back. But you are going to need to change a few things. First you need to get your parameters right, so remove this one:
<asp:QueryStringParameter Name="SId" QueryStringField="SId" />
and put this one in its place:
<asp:ControlParameter ControlID="tbId" PropertyName="Text" Name="tbId" />
and now remove all of this code:
protected void btnSearch_Click(object sender, EventArgs e)
{
string SId = tbId.Text;
SqlDataSource1.DataBind();
}
and now the SqlDataSource is tied directly to the control value, so when the user puts in a new control value and clicks the button it will be applied and new data retrieved without any of your interaction.

Categories