[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1210
  • Last Modified:

dropdownlist

Hi ,
 

 I have a drop downlist in a detailsview  called Active . I want to set the list as 'Y' or 'N'

I have written this code . But it would only show the default value 'Y' at runtime. It would not show  the value 'N'.

Please look at the code below and help me in resolving the problem.



<asp:TemplateField HeaderText="ACTIVE" >
<ItemTemplate>

&nbsp;

<asp:Label ID="Label3"  runat="server" Text=" "></asp:Label>

<asp:DropDownList ID="DropDownList1"  runat="server" AutoPostBack="False">

<asp:ListItem> N</asp:ListItem>
<asp:ListItem> Y</asp:ListItem>
 </asp:DropDownList>

</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID ="DropDownList1" runat = "Server" DataValueField ="Active" DataTextField = "Active" DataSourceID = "SqlDataSource2" Selectedvalue = '<%#Bind("Active") %>' />
 </EditItemTemplate>

</asp:TemplateField>



Thanks,

Sreekanth
0
Sreekanth_Nagabandi
Asked:
Sreekanth_Nagabandi
  • 6
  • 5
1 Solution
 
kumar_jacCommented:
Hi,
 Instead of that

in Page load please copy and paste the code

if(IsPostBack==false)
 {
      DropDownList1.Items.Add("N");
      DropDownList1.Items.Add("Y");
 }

 Please try like this and if any problem please get back.
 
0
 
DonKylesCommented:
I think inside the ItemTemplate, you just want to display the value. But when you want to edit the item, it changes to DropDownList for user to select.

The correct code would be  

<asp:TemplateField HeaderText="ACTIVE" >
         <ItemTemplate>
                <asp:Label ID="Label3"  runat="server" Text="<%# Bind("Active") %>"></asp:Label>
         </ItemTemplate>
         <EditItemTemplate>
                <asp:DropDownList ID="DropDownList1" runat="Server" DataValueField="Active" DataTextField="Active" DataSourceID="SqlDataSource2"
                          Selectedvalue = '<%#Bind("Active") %>' />
          </EditItemTemplate>
</asp:TemplateField>

I'm not so sure the value of <%#Bind("Active") %>, the value should be Y/N otherwise on the item template will display something else.
If the <%#Bind("Active") %> value is not Y/N, you have to rewrite the SQL Statement to  something like below

SELECT ID, CASE WHEN Active = '1' THEN 'Y'
            ELSE 'N' END AS 'Active'
FROM tableName
WHERE  [condition]
GROUP BY CASE WHEN Active = '1' THEN 'Y'
                ELSE 'N' END

If you are not so sure please give me your entire code and I can help you out.
0
 
Sreekanth_NagabandiAuthor Commented:
Hi,

  This my entire code for the dropdown list . I have tried yours but i get a message , error in creating control.

  I will explain what the below code does, I get the value from the gridview and bind it to the details view in a drop down list . It only gets the value . But say  for example, I got the value  'N' as No , and i want it to update to 'Y' , it would only display one value in the list and apparently i cannot update the value to 'Y'. I want 'Y' or 'N' to display in the list.



aspx code :-


<asp:TemplateField HeaderText="ACTIVE" >
<ItemTemplate>
&nbsp;

<asp:Label ID="Label3"  runat="server" Text=" "></asp:Label>


<asp:DropDownList ID="DropDownList1"  runat="server" AutoPostBack="False">

<asp:ListItem> N</asp:ListItem>
<asp:ListItem> Y</asp:ListItem>
 </asp:DropDownList>

</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID ="DropDownList1" runat = "Server" DataValueField ="Active" DataTextField = "Active" DataSourceID = "SqlDataSource2" Selectedvalue = '<%#Bind("Active") %>' />
 </EditItemTemplate>

</asp:TemplateField>


Thanks,

Sreekanth

0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
DonKylesCommented:
Ok try this

<asp:TemplateField HeaderText="ACTIVE" >
<ItemTemplate>
         <asp:Label ID="Label3"  runat="server" Text=" "></asp:Label>
         <asp:DropDownList ID="ddlItem"  runat="server" AutoPostBack="False">
              <asp:ListItem> N</asp:ListItem>
              <asp:ListItem> Y</asp:ListItem>
          </asp:DropDownList>
      </ItemTemplate>
     <EditItemTemplate>
         <asp:DropDownList ID ="DropDownList1" runat = "Server" DataValueField ="Active" DataTextField = "Active" DataSourceID = "SqlDataSource2"
           Selectedvalue = '<%#Bind("Active") %>' />
     </EditItemTemplate>
</asp:TemplateField>

In the code part

protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
         if (e.Row.RowType == DataControlRowType.DataRow)
        {
                if (DataBinder.Eval(e.Row.DataItem, "Active").ToString() == "Y")      // replace with the value of Active
                {
                      Control objC = e.Row.FindControl("ddlItem");
                      DropDownList ddlTemp = (DropDownList)objC;
                      ddlTemp.SelectedValue = "Y";
                }
         }
}

This should work for your issue. Since I dont' know your active value I assume as 'Y'.

0
 
Sreekanth_NagabandiAuthor Commented:
Hi ,

  I appreciate your help . But I have already binded the data in to details view from the grid view. The data is in the details view now.

Now , I have to update the record . To update I should have another value in the dropdown list which is a boolean value('Y' or 'N').

So, You have sent the code  if (e.Row.RowType == DataControlRowType.DataRow) . I presume, the e.row property is not available in the details view.

I wonder if you can send me the DataBound event for the details view please.

Thanks,
Sreekanth Nagabandi.
0
 
DonKylesCommented:
I'm not so sure your problem now. Give me some output example, like

ID        Name          Active
1           Ab               Y
2           Cd               N
3           Ef                 Y


when update

ID                              Name                Active
cannt change               textbox              dropdownlist Y and N

something like this, then I can hlep you
0
 
Sreekanth_NagabandiAuthor Commented:
HI,

I appreciate your help . Thats obsolutely  right .  The update operation happens in the details view.

Thanks,

Sreekanth Nagabandi
0
 
Sreekanth_NagabandiAuthor Commented:


Hi ,

   I Wonder if someone can look in to my query and send me the code please.

Thanks,

Sreekanth.
0
 
DonKylesCommented:
where is your query? Send me the complete code for gridview and sql then I can help you.
0
 
Sreekanth_NagabandiAuthor Commented:
Hi,

 Its very simple. Sorry to baffle you. I have binded the record on to details view from the grid view successfully.
The bussiness logic is to implement using the dropdown list  for 'active' , 'regulatory_alarm' , auto_acknowledge columns.As it stands, i can only see one value, 'Y' or 'N' . So apparently i cannot change the value to update the record.
I want both the values 'Y' and 'N' to be diaplayed in the dropdownlist . So i presume , both the values has to be binded.


Query:-    I want  the values 'Y' and 'N' to be displayed in to the dropdownlist.
Please look at my aspx code and reply ASAP as there is a deadline today.





AlarmModdetails.aspx:-


<%@ Page Language="VB" MasterPageFile="~/MACTMasterNT.master" AutoEventWireup="false" SmartNavigation="true" CodeFile="AlarmModDetails.aspx.vb" Inherits="AlarmModDetails" title="MACT AlarmModDetails Page" %>
      <%@ MasterType VirtualPath="MACTMaster.master" %>
     
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

<%@ Register Assembly="RadTabStrip.Net2" Namespace="Telerik.WebControls" TagPrefix="radTS" %>
 
    <radTS:RadTabStrip ID="radtabstrip1" runat="server" > 
    <Tabs>
        <radTS:Tab ID="Tab1" Text="General" NavigateUrl="AlarmModDetails.aspx" runat="server"></radTS:Tab>
        <radTS:Tab ID="Tab4" Text="Definition" NavigateUrl="AlarmDefDetails.aspx" runat="server"></radTS:Tab>
        <radTS:Tab ID="Tab2" Text="Update Permissions" NavigateUrl="AlarmDefUpdates.aspx" runat="server"></radTS:Tab>
        <radTS:Tab ID="Tab3" Text="Onset Notification" NavigateUrl="AlarmDefOnset.aspx" runat="server"></radTS:Tab>
       
        </Tabs>
            </radTS:RadTabStrip>
           
      <asp:DetailsView ID="AlarmsDetails" DataKeyNames="Alarm_ID" OnItemUpdated="AlarmsDetails_ItemUpdated" DataSourceID="SqlDataSource2" AutoGenerateEditButton="True" runat="server"  
    OnItemInserted="AlarmsDetails_ItemInserted"  Height="263px" Width="334px" AutoGenerateRows="False"  AllowPaging="True"  DefaultMode="Edit"  HorizontalAlign="Center">
   
   <Fields>
      <asp:BoundField ReadOnly="True" HeaderText="AlARM ID" InsertVisible="False" DataField="Alarm_ID"
    SortExpression="Alarm_ID"></asp:BoundField>
    <asp:BoundField ReadOnly="True" HeaderText="UNIT"  InsertVisible="False" DataField="Unit_ID"
    SortExpression="Unit_ID"></asp:BoundField>

      <asp:BoundField DataField="DESCRIPTION" HeaderText="DESCRIPTION" SortExpression="DESCRIPTION" />
      <asp:BoundField DataField="MODE" HeaderText="MODE" SortExpression="MODE" />
     
     
     
<asp:TemplateField HeaderText="ACTIVE" >
<ItemTemplate>
&nbsp;

<asp:Label ID="Label3"  runat="server" Text=" "></asp:Label>


<asp:DropDownList ID="ddlItem"  runat="server" AutoPostBack="true">

<asp:ListItem> N</asp:ListItem>
<asp:ListItem> Y</asp:ListItem>
 </asp:DropDownList>

</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID ="DropDownList1" runat = "Server" DataValueField ="Active" DataTextField = "Active" DataSourceID = "SqlDataSource2" Selectedvalue = '<%#Bind("Active") %>' />
 </EditItemTemplate>

</asp:TemplateField>


<asp:TemplateField HeaderText="RegulatoryAlarm" >
<ItemTemplate>
&nbsp;

<asp:Label ID="Label4"  runat="server" Text=" "></asp:Label>


<asp:DropDownList ID="DropDownList2"  runat="server" AutoPostBack="False">

<asp:ListItem> N</asp:ListItem>
<asp:ListItem> Y</asp:ListItem>
 </asp:DropDownList>

</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID ="DropDownList2" runat = "Server" DataValueField ="Regulatory_Alarm" DataTextField = "Regulatory_Alarm" DataSourceID = "SqlDataSource2" Selectedvalue = '<%#Bind("Regulatory_Alarm") %>' />
 </EditItemTemplate>

</asp:TemplateField>





<asp:TemplateField HeaderText="AutoAcknowledge" >
<ItemTemplate>
&nbsp;

<asp:Label ID="Label5"  runat="server" Text=" "></asp:Label>


<asp:DropDownList ID="DropDownList3"  runat="server" AutoPostBack="False">

<asp:ListItem> N</asp:ListItem>
<asp:ListItem> Y</asp:ListItem>
 </asp:DropDownList>

</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID ="DropDownList3" runat = "Server" DataValueField ="Auto_Acknowledge" DataTextField = "Auto_Acknowledge" DataSourceID = "SqlDataSource2" Selectedvalue = '<%#Bind("Auto_Acknowledge") %>' />
 </EditItemTemplate>

</asp:TemplateField>


       
  </Fields>
  </asp:DetailsView>
   
  <br >
   
    <asp:Panel ID="Panel1" runat="server" BackColor="Transparent" Height="723px" Style="z-index: 101;
            left: 245px; position: absolute; top: -101px" Width="688px" HorizontalAlign = "Center" Color="LightSteelBlue" Enabled="False" ForeColor="SteelBlue" BorderColor="#C0C0FF" >
             
        &nbsp;&nbsp;&nbsp;<td valign="top">
            &nbsp;&nbsp;

    &nbsp;&nbsp;
   
   
<asp:SqlDataSource ID="SqlDataSource2" ConnectionString="Data Source=SPT-SQL02;Integrated Security=True; Initial Catalog=CMS; uid=CMS;pwd=MACT"
            runat="server" SelectCommand="SELECT [Alarm_ID], [Unit_ID], [Description], [Mode], [Active], [Regulatory_Alarm], [Auto_Acknowledge] FROM [Alarm_Definition] WHERE ([Alarm_ID] = @Alarm_ID )"
                           UpdateCommand="UPDATE [Alarm_Definition] SET [Description] = @Description, [Mode] = @Mode, [Active] = @Active, [Regulatory_Alarm] = @Regulatory_Alarm, [Auto_Acknowledge] = @Auto_Acknowledge WHERE [Alarm_ID] = @Alarm_ID  ">
                           
               <SelectParameters>
                   <asp:QueryStringParameter Name="Alarm_ID" QueryStringField="Alarm_ID" Type="Int32" DefaultValue="0" />
                </SelectParameters>
                             
          <UpdateParameters>
                           
              <asp:Parameter Name="Description" Type="String" />
              <asp:Parameter Name="Mode" Type="Int32" />
              <asp:Parameter Name="Active" Type="String" DefaultValue="<%=N %>" />
              <asp:Parameter Name="Regulatory_Alarm" Type="String" />
              <asp:Parameter Name="Auto_Acknowledge" Type="String" />
              <asp:Parameter Name="Alarm_ID" Type="Int32" />
              <asp:Parameter Name="Unit_ID" Type="Int32" />
               
            </UpdateParameters>

          </asp:SqlDataSource>
           
             
           
           
    </asp:Panel>
        &nbsp;</asp:Content>



Thanks,

Sreekanth Nagabandi.
0
 
DonKylesCommented:
I think you miss understand the concept, i'm not so sure about the detail view but I have written amost the same as your issue.

Under the EditItemTemplate, you need those dropdownlist to display 'Y' and 'N' just add <asp:ListItem>N</asp:ListItem> into them and take out the DataSource, but I dont' know the value of your Active,  Regulartory_Alarm and Auto_Acknowledge. But the correct code would be below

<EditItemTemplate>
    <asp:DropDownList ID ="DropDownList2" runat = "Server" DataValueField ="Regulatory_Alarm"
           Selectedvalue = '<%#Bind("Regulatory_Alarm") %>' >
        <asp:ListItem> N</asp:ListItem>
        <asp:ListItem> Y</asp:ListItem>
    </asp:DropDownList>
 </EditItemTemplate>

here is my gridview code (since I cannot run to see the error, but this should be very close)
-----------------------------------------------------------------------------------------------

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="2" DataSourceID="SqlDataSource2"
    Font-Names="Tahoma" Font-Size="12px" ForeColor="#333333" GridLines="None" AllowSorting="True">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<Columns>
    <asp:CommandField HeaderText="Edit" ShowEditButton="True" ButtonType="Link" >
    </asp:CommandField>
    <asp:BoundField ReadOnly="True" HeaderText="AlARM ID" InsertVisible="False" DataField="Alarm_ID"
        SortExpression="Alarm_ID"></asp:BoundField>
    <asp:BoundField ReadOnly="True" HeaderText="UNIT"  InsertVisible="False" DataField="Unit_ID"
        SortExpression="Unit_ID"></asp:BoundField>
    <asp:BoundField DataField="DESCRIPTION" HeaderText="DESCRIPTION" SortExpression="DESCRIPTION" />
    <asp:BoundField DataField="MODE" HeaderText="MODE" SortExpression="MODE" />
    <asp:TemplateField HeaderText="ACTIVE" >
        <ItemTemplate>
            <asp:Label ID="Label3"  runat="server" Text='<%#Bind("Active") %>'></asp:Label>
        </ItemTemplate>
        <EditItemTemplate>
        <asp:DropDownList ID="DropDownList1" runat="Server" Selectedvalue='<%#Bind("Active") %>' >
                <asp:ListItem> N</asp:ListItem>
                <asp:ListItem> Y</asp:ListItem>
            </asp:DropDownList>
        </EditItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="RegulatoryAlarm" >
        <ItemTemplate>
            <asp:Label ID="Label4"  runat="server" Text='<%#Bind("Regulatory_Alarm") %>'></asp:Label>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:DropDownList ID="DropDownList2" runat="Server" Selectedvalue='<%#Bind("Regulatory_Alarm") %>' >
                <asp:ListItem> N</asp:ListItem>
                <asp:ListItem> Y</asp:ListItem>
            </asp:DropDownList>
        </EditItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="AutoAcknowledge" >
        <ItemTemplate>
            <asp:Label ID="Label5"  runat="server" Text='<%#Bind("Auto_Acknowledge") %>' ></asp:Label>
        </ItemTemplate>
        <EditItemTemplate>
        <asp:DropDownList ID ="DropDownList3" runat="Server" Selectedvalue='<%#Bind("Auto_Acknowledge") %>' >
                <asp:ListItem> N</asp:ListItem>
                <asp:ListItem> Y</asp:ListItem>
            </asp:DropDownList>
        </EditItemTemplate>
    </asp:TemplateField>
</Columns>
<RowStyle BackColor="White" />
<PagerStyle ForeColor="White" HorizontalAlign="Center" BackColor="#2461BF" />
<HeaderStyle BackColor="#ACD9B9" Font-Bold="True" ForeColor="Black" Height="25px" />
<EditRowStyle BackColor="#EFF3FB" />
<AlternatingRowStyle BackColor="#E2F0E2" />                            
</asp:GridView>
           
<asp:SqlDataSource ID="SqlDataSource2" ConnectionString="Data Source=SPT-SQL02;Integrated Security=True; Initial Catalog=CMS; uid=CMS;pwd=MACT"
    runat="server"
    SelectCommand="SELECT [Alarm_ID], [Unit_ID], [Description], [Mode], [Active], [Regulatory_Alarm], [Auto_Acknowledge]
                FROM [Alarm_Definition] WHERE ([Alarm_ID] = @Alarm_ID )"
    UpdateCommand="UPDATE [Alarm_Definition]
                    SET [Description] = @Description, [Mode] = @Mode, [Active] = @Active, [Regulatory_Alarm] = @Regulatory_Alarm,
                    [Auto_Acknowledge] = @Auto_Acknowledge WHERE [Alarm_ID] = @Alarm_ID  ">                      
    <SelectParameters>
        <asp:QueryStringParameter Name="Alarm_ID" QueryStringField="Alarm_ID" Type="Int32" DefaultValue="0" />
    </SelectParameters>              
    <UpdateParameters>
        <asp:Parameter Name="Description" Type="String" />
        <asp:Parameter Name="Mode" Type="Int32" />
        <asp:Parameter Name="Active" Type="String" />
        <asp:Parameter Name="RegulatoryAlarm" Type="String" />
        <asp:Parameter Name="AutoAcknowledge" Type="String" />
        <asp:Parameter Name="Alarm_ID" Type="Int32" />
        <asp:Parameter Name="Unit_ID" Type="Int32" />
    </UpdateParameters>
  </asp:SqlDataSource>
</asp:Content>

------------------------------------------------------------------
Girdview will display the result like below

Edit       Alarm ID    Unit     Description       MOde     Active      Regulartory Alarm         AutoAcknowledge
edit          1             12      Alarm ID is 1      A           Y                    N                                 Y
Edit          2             15      Alarm ID is 2      B           N                    Y                                 N
Edit          3             14      Alarm ID is 3      C           Y                    Y                                 N

when you click on Edit link on a particular row

Edit                   Alarm ID    Unit     Description       Mode      Active                       Regulartory Alarm         AutoAcknowledge
Update cancle         1           12      Alarm ID is 1      A          dropdownlist N/Y      dropdownlist N/Y           dropdownlist N/Y
Edit                        2           15      Alarm ID is 2      B           N                                         Y                                 N
Edit                        3           14      Alarm ID is 3      C           Y                                         Y                                 N




0
 
Sreekanth_NagabandiAuthor Commented:
Hi ,


  Thats fine. I appreciate your help and did figure out the problem. You know there was an error in the connection string and it returns the data to the data base.

Thanks,

Sreekanth Nagabandi.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now