locked
control parameter DropDownList in gridview RRS feed

  • Question

  • User-1998474842 posted

    Hi Guys;

    Why DropDownListJor selected value did not save to Databases? 

        protected void Product_Item_1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow && Product_Item_1.EditIndex == e.Row.RowIndex)
            {
    
            }
        }
    
        protected void Product_Item_1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            Product_Item_1.EditIndex = e.NewEditIndex;
        }
        protected void Product_Item_1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            GridViewRow row = (GridViewRow)Product_Item_1.Rows[e.RowIndex];
            string DropDownListJor = (Product_Item_1.Rows[e.RowIndex].FindControl("DropDownListJor") as DropDownList).SelectedItem.Value;
            
            Product_Item_1.EditIndex = -1;
    
        }
                <asp:GridView ID="Product_Item_1" OnRowDataBound="Product_Item_1_RowDataBound" OnRowEditing="Product_Item_1_RowEditing" OnRowUpdating="Product_Item_1_RowUpdating" Visible="false" runat="server" DataSourceID="AccessDataSource2" AutoGenerateColumns="False" DataKeyNames="COBULEAPID" GridLines="None" ShowHeader="False" >
                    <Columns>
                        <asp:TemplateField>
                            <EditItemTemplate>
                                <div style="margin-right: auto; margin-left: auto; display: block; text-align: center; margin-top: 20px;">
                                    ژماره‌ی ته‌له‌فۆنی خاوه‌ن شوقه‌ داخڵ بکه ،‌ مه‌کته‌بی بۆ موڵک له ماوه‌ی 24 کاتژمێردا په‌یوه‌ندی به‌‌ به‌ڕیزته‌وه‌ ئه‌کات.
                                    <div style="direction: ltr; text-align: center;">
                                        <br />
                                        <asp:RegularExpressionValidator ID="RegularExpressionValidator1" ValidationGroup="Save" runat="server" Display="dynamic" SetFocusOnError="true" ControlToValidate="tPhone_No" ErrorMessage="ژماره‌ی ته‌له‌فۆن به‌ته‌واوی بنوسه‌" ValidationExpression="[0-9]{11}"></asp:RegularExpressionValidator>
                                        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ValidationGroup="Save" runat="server" Display="dynamic" SetFocusOnError="true" ControlToValidate="tPhone_No" ErrorMessage="ژماره‌ی ته‌له‌فۆن‌">ژماره‌ی ته‌له‌فۆن</asp:RequiredFieldValidator>
                                        <br />
                                        <br />
                                        <asp:TextBox ID="tPhone_No" type="tel" runat="server" Font-Size="14px" placeholder="‌‌" BorderColor="Red" BorderStyle="Dotted" BorderWidth="1px" AutoCompleteType="Disabled" MaxLength="11" Width="50%" Text='<%# Bind("Phone_No") %>'></asp:TextBox>
                                    </div>
                                    <div style="direction: rtl; text-align: center;">
                                        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" ValidationGroup="Save" runat="server" ControlToValidate="DropDownListJor" InitialValue="none" ErrorMessage="جۆری عه‌رزکردن له‌ بۆ موڵک." />
                                        <br />
                                        <br />
                                        <asp:DropDownList runat="server" ID="DropDownListJor" BorderColor="Red" BorderStyle="Dotted" BorderWidth="1px" Width="50%">
                                            <asp:ListItem Value="none">ڕۆژێک دیاری بکه‌‌</asp:ListItem>
                                            <asp:ListItem Value="1">بۆ به‌کرێ</asp:ListItem>
                                            <asp:ListItem Value="2">بۆ فرۆشتن‌</asp:ListItem>
                                        </asp:DropDownList>
                                    </div>
                                    <br />
                                    <asp:LinkButton ValidationGroup="Save" ForeColor="green" ID="SendPhone" runat="server" CausesValidation="True" CommandName="Update" Text="ناردن بۆ نوسینگه‌ی بۆ موڵکم"></asp:LinkButton>
                                    <br />
                                    <br />
                                </div>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <div class="s-12 m-12 l-12 margin-bottom" style="margin-right: auto; margin-left: auto; display: block; text-align: center; margin-top: 20px;">
                                    <asp:LinkButton ForeColor="green" ID="TrytoSendPhone" runat="server" CausesValidation="False" CommandName="Edit" Text="بۆ فرۆشتن یان به‌کرێ دان ئه‌م یه‌که‌یه کلیک لێره ‌بکه‌"></asp:LinkButton>
                                </div>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                    <EmptyDataTemplate>
                        <div style="margin-right: auto; margin-left: auto; display: block; text-align: center; margin-top: 20px; color: orange;">
                            ئه‌م باڵه‌خانه‌یه‌ به‌کرێ یان بۆ فرۆشتن عه‌ڕزکرا بۆ هه‌رپرسیارێک په‌یوه‌ندی بکه‌ به‌ .‌
                            <br />
                            <br />
                            <br />
                            <asp:LinkButton ForeColor="Green" ID="LinkButton1" runat="server" OnClick="Chainge_Location_Click">گه‌ڕانه‌وه‌ بۆ سه‌ره‌تا</asp:LinkButton>
                        </div>
                    </EmptyDataTemplate>
                </asp:GridView>
                <asp:AccessDataSource
                    ID="AccessDataSource2"
                    runat="server"
                    DataFile="~/App_Data/DAWABKADBKU.mdb"
                    SelectCommand="SELECT * FROM [Dawabka_Com_Buildings_Levels_Apart] WHERE (([COID] = ?) AND ([COBUID] = ?) AND ([COBULEID] = ?) AND ([COBULEAPID] = ?) AND ([Statuse] = ?)) ORDER BY [COBULEAPID] DESC"
                    UpdateCommand="UPDATE [Dawabka_Com_Buildings_Levels_Apart] SET [COID] = ?, [COBUID] = ?, [COBULEID] = ?, [Statuse] = ?, [Phone_No] = ?, [Jor] = ? WHERE [COBULEAPID] = ?">
                    <SelectParameters>
                        <asp:QueryStringParameter Name="COID" QueryStringField="COID" Type="Int32" />
                        <asp:QueryStringParameter Name="COBUID" QueryStringField="COBUID" Type="Int32" />
                        <asp:QueryStringParameter Name="COBULEID" QueryStringField="COBULEID" Type="Int32" />
                        <asp:QueryStringParameter Name="COBULEAPID" QueryStringField="COBULEAPID" Type="Int32" />
                        <asp:Parameter DefaultValue="0" Name="Statuse" Type="Int32" />
                    </SelectParameters>
                    <UpdateParameters>
                        <asp:QueryStringParameter Name="COID" QueryStringField="COID" Type="Int32" />
                        <asp:QueryStringParameter Name="COBUID" QueryStringField="COBUID" Type="Int32" />
                        <asp:QueryStringParameter Name="COBULEID" QueryStringField="COBULEID" Type="Int32" />
                        <asp:Parameter DefaultValue="1" Name="Statuse" Type="Int32" />
                        <asp:Parameter Name="Phone_No" Type="String"/>
                        <asp:controlparameter Name="Jor" controlid="DropDownListJor" PropertyName="SelectedValue" Type="String"/>
                        <asp:Parameter Name="COBULEAPID" Type="Int32" />
                    </UpdateParameters>
                </asp:AccessDataSource>
    

    Thursday, April 18, 2019 5:41 PM

Answers

  • User288213138 posted

    Hi  Peshang M.Hussien,
     
    I looked at your code and found that the reason for not updating was that the update command in AccessDataSource was not executed. You can add a Bind () method to DropDownList, which updates the data passed by AccessDataSource.  You can refer to my code below.
     
    The code:

    <asp:GridView ID="GridView1" runat="server" Width="300px" AutoGenerateColumns="False" DataKeyNames="ID" AutoGenerateEditButton="true" 
                    DataSourceID="SqlDataSource1">
                      <Columns>
                                            
                          
    
                            <asp:TemplateField HeaderText="Name" >
                                <ItemTemplate>
                                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:DropDownList DataSourceID="SqlDataSource1" ID="DdlCategories" DataTextField="Name" DataValueField="Name"
                                        runat="server"   SelectedValue='<%# Bind("Name") %>' AppendDataBoundItems="true">  
                                       
                                    </asp:DropDownList>
                                    
                                </EditItemTemplate>
                            </asp:TemplateField>
                         
                      </Columns>
    
                </asp:GridView>
      
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:constr888 %>" DeleteCommand="DELETE FROM [Products] WHERE [Id] = @Id" InsertCommand="INSERT INTO [Products] ([Name]) VALUES (@Name)" ProviderName="System.Data.SqlClient" SelectCommand="SELECT [Id], [Name] FROM [Products]" UpdateCommand="UPDATE [Products] SET [Name] = @Name WHERE [Id] = @Id">
                    <DeleteParameters>
                        <asp:Parameter Name="Id" Type="Int32" />
                    </DeleteParameters>
                    <InsertParameters>
                        <asp:Parameter Name="Name" Type="String" />
                    </InsertParameters>
                    <UpdateParameters>
                        <asp:Parameter Name="Name" Type="String" />
                        <asp:Parameter Name="Id" Type="Int32" />
                    </UpdateParameters>
                </asp:SqlDataSource>

    The Result:


     
    Best Regard,
    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 19, 2019 9:38 AM