locked
Gridview loading RRS feed

  • Question

  • User66371569 posted

    Hi people  

    I am using gridview    as following:

    <asp:GridView ID="GridView4" runat="server" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" style="font-weight: 700" Width="852px" PageIndex="5" Height="180px">
    <Columns>
    <asp:TemplateField HeaderText="عدد الكوبونات">
    <ItemTemplate>
    <asp:TextBox ID="cobnumbers" runat="server" CssClass="txt" ReadOnly="True" Width="56px"></asp:TextBox>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="تاريخ الانتهاء">
    <ItemTemplate>
    <asp:TextBox ID="EndDate" runat="server" CssClass="txt" ReadOnly="True" Width="92px"></asp:TextBox>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="تاريخ البداية">
    <ItemTemplate>
    <asp:TextBox ID="Startdate" runat="server" OnTextChanged="Startdate_TextChanged" Width="83px" CssClass="txt"></asp:TextBox>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="الكوبون">
    <ItemTemplate>
    <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="CoboneName" DataValueField="CoboneID" style="font-weight: 700" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" CssClass="select">
    </asp:DropDownList>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="رقم الكوبون">
    <ItemTemplate>
    <asp:TextBox ID="Coboneno" runat="server" Height="21px" Width="60px" CssClass="txt"></asp:TextBox>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
    <HeaderStyle BackColor="#94BE10" Font-Bold="True" Font-Size="12pt" ForeColor="White" />
    <PagerStyle BorderColor="#8CAC21" BorderStyle="Solid" ForeColor="#8C4510" HorizontalAlign="Center" />
    <RowStyle BackColor="White" BorderColor="#99CC00" Font-Size="11pt" ForeColor="Black" HorizontalAlign="Center" />
    <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#FFF1D4" />
    <SortedAscendingHeaderStyle BackColor="#B95C30" />
    <SortedDescendingCellStyle BackColor="#F1E5CE" />
    <SortedDescendingHeaderStyle BackColor="#93451F" />
    </asp:GridView>

    My cod in textbox change event


    Protected Sub Startdate_TextChanged(sender As Object, e As EventArgs)
    Dim startDate As TextBox = CType(sender, TextBox) ' get the startDate from sender
    Dim endDate As TextBox = CType(startDate.NamingContainer.FindControl("EndDate"), TextBox)
    Dim cobnumbers As TextBox = CType(startDate.NamingContainer.FindControl("cobnumbers"), TextBox)
    Dim MyDate As Date = DateTime.ParseExact(startDate.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture)

    Dim DaysInMonth As Integer = Date.DaysInMonth(MyDate.Year, MyDate.Month)

    Dim LastDayInMonthDate As Date = New Date(MyDate.Year, MyDate.Month, DaysInMonth)
    EndDate.Text = LastDayInMonthDate.ToString("dd/MM/yyyy")


    cobnumbers.Text = DateDiff(DateInterval.Day, DateTime.ParseExact(startDate.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture), DateTime.ParseExact(EndDate.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture))

    End Sub

    My code in Dropdwonlist event

    Protected Sub DropDownList1_SelectedIndexChanged(sender As Object, e As EventArgs)
    Dim dropdown = CType(sender, DropDownList) ' get the dropdown
    Dim startDate As TextBox = CType(dropdown.NamingContainer.FindControl("Startdate"), TextBox)
    Dim endDate As TextBox = CType(dropdown.NamingContainer.FindControl("EndDate"), TextBox)
    Dim coboneno As TextBox = CType(dropdown.NamingContainer.FindControl("Coboneno"), TextBox)
    If dropdown.SelectedValue = "1" Then
    startDate.Enabled = True
    endDate.Enabled = True
    coboneno.Enabled = True
    Else
    startDate.Enabled = False
    endDate.Enabled = False
    coboneno.Enabled = False
    startDate.Text = ""
    endDate.Text = ""
    coboneno.Text = ""

    End If

    I have problems

    1- drop down list   when chamged  effects all rows      I  don't want that  I want  row which I change only get effect

    2- textbx  same situation  and also  once I change in textbox called startdate    this textbox gets clear  

    3- when I press save button    gridview  postback and all my modification gone   and saved indatabase null

    How can I solve all that   please  help me

    Saturday, September 29, 2018 11:05 AM

All replies

  • User475983607 posted

    Start by reading the GridView API documentation and trying the code samples.  The GridView is pretty complex and requires practice. 

    https://msdn.microsoft.com/en-us/library/ms972948.aspx?f=255&MSPPError=-2147217396

    The main issue with your approach the dropdown list is specifically coded to auto post back on change.  Depending on how you've code the form the GridView most likely reverts back to its original state.

    Anyway. reading the docs should help.

    Saturday, September 29, 2018 12:34 PM
  • User-1716253493 posted

    Seem like you have call gv4,databind() every postback

    Saturday, September 29, 2018 5:23 PM
  • User66371569 posted
    yes i have also tamplete filelds same above i have more than 4 days coulden't solve it
    Saturday, September 29, 2018 7:06 PM
  • User-1716253493 posted

    You should call databind in not ispostback only

    binding the grid will reset the grid

    Sunday, September 30, 2018 4:54 AM
  • User66371569 posted

    I will explain for u  

    I have tha t gride view  what I want   Is edit  in textboxes    and press save  I want all changes saved   but when I press save   all gridview get postback an reset

    and  also same for dropsownlist changes     effect all rows and also postback pgae

    How can I solve that

    Sunday, September 30, 2018 5:56 AM
  • User475983607 posted

    I will explain for u  

    I have tha t gride view  what I want   Is edit  in textboxes    and press save  I want all changes saved   but when I press save   all gridview get postback an reset

    and  also same for dropsownlist changes     effect all rows and also postback pgae

    How can I solve that

    The first step is learning the GridView and Web Forms basics.  The GridView has been around a long time and there are endless tutorials and examples.  See my first post and go through the links...

    I recommend focusing on learning the basics rather than trying to solve specific programming problems.   It will be much easier to solve programming problems in ASP Web Forms once you understand the Web Forms basics.

    Sunday, September 30, 2018 12:28 PM
  • User-1716253493 posted

    First, dont't call databind in postback

    Save the data as what you want, call databind after save data

    Sunday, September 30, 2018 11:05 PM
  • User1724605321 posted

    Hi thepast ,

    You haven't post the page load event so we can't confirm whether you rebind the gridview on every postback , you can put the bind gridview logic inside the Not Postback logic :

    If Not Me.IsPostBack Then
            Me.BindGrid()
    End If

    You can refer to below code samples for Gridview inline editing :


    https://www.aspsnippets.com/Articles/Edit-Update-and-Delete-in-ASPNet-GridView-with-AutoGenerateColumns-True-using-C-and-VBNet.aspx

    https://channel9.msdn.com/Blogs/ASP-NET-Site-Videos/how-do-i-enable-inline-gridview-editing 

    Best Regards,

    Nan Yu

    Monday, October 1, 2018 3:20 AM
  • User66371569 posted
    If i dont want yo use Editing inside Grideview I have button Save to Update Can i
    Monday, October 1, 2018 4:29 AM
  • User1724605321 posted

    Hi thepast ,

    If i dont want yo use Editing inside Grideview I have button Save to Update Can i

    If you are opening edit page to edit the page and not editing inside gridview . When clicking save button in edit page , you can redirect user to index page and it will re-bind the newest data from database to gridview . Or do you want to still in edit page after clicking save button ?

    Best Regards,

    Nan Yu

    Monday, October 1, 2018 6:57 AM