 |
|
คือว่า ในตอนแรกทำการ insert ข้อมูลลง ฐานข้อมูล โดยกดปุ่ม add ไปแล้ว กลับมาที่หน้าเดิม แล้วทำการ update ค่าอื่นต่อ โดยกดปุ่ม update ใน gridview อะค่ะ มัน update ได้ปกติ แต่ข้อมูลที่เคย add ไปแล้ว มัน add insert ลงฐานข้อมูลอีกรอบนึงอ่าค่ะ ไม่เข้าใจว่าทำไม ต้อง clear ค่า ทิ้งไหมค่ะ แล้วทำยังไงอ่าค่ะ ขอบคุณมากๆค่ะ
code เป็นแบบนี้ค่ะ
นี่เป็นส่วน html ค่ะ
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
ShowFooter="True"
OnRowEditing="modEditCommand"
OnRowCancelingEdit="modCancelCommand"
OnRowUpdating="modUpdateCommand" OnRowCommand="myGridView_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Type ">
<EditItemTemplate>
<asp:TextBox class="textbox_tableblue" size="10" ID="name_rate" runat="server" Text='<%# Bind("Name_RefType") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Name_RefType") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox id="txtAddType" size="10" runat="server"></asp:TextBox>
</FooterTemplate>
<HeaderStyle BackColor="LightCyan" BorderColor="LightCyan" Width="150px"
ForeColor="Black" />
<ItemStyle BackColor="LightCyan" BorderColor="LightCyan" Height="25px" />
<FooterStyle BackColor="LightCyan" BorderColor="LightCyan" Height="25px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Rate">
<EditItemTemplate>
<asp:TextBox class="textbox_tableblue" size="10" ID="rate" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Rate_Refund") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Rate_Refund") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox id="txtAddrate" size="10" runat="server"></asp:TextBox>
</FooterTemplate>
<HeaderStyle BackColor="LightCyan" BorderColor="LightCyan" Width="100px"
ForeColor="Black" />
<ItemStyle BackColor="LightCyan" BorderColor="LightCyan" />
<FooterStyle BackColor="LightCyan" BorderColor="LightCyan" Height="25px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Status">
<EditItemTemplate>
<table class="tableblue">
<tr><td align="left">
<asp:RadioButtonList ID="rdlStatusEdit" runat="server" >
<asp:ListItem Value="1" Selected="True">use</asp:ListItem>
<asp:ListItem Value="0">not use</asp:ListItem>
</asp:RadioButtonList>
</tr></td>
</table>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("statusStr") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<table class="tableblue">
<tr><td align="left">
<asp:RadioButtonList ID="rdlStatus" runat="server" >
<asp:ListItem Value="1" Selected="True">use</asp:ListItem>
<asp:ListItem Value="0">not use</asp:ListItem>
</asp:RadioButtonList>
</td><td>
<asp:Button class="Buttonblack" id="btnAdd" runat="server" Text="Add" CommandName="Add"></asp:Button>
</td></tr>
</table>
</FooterTemplate>
<HeaderStyle BackColor="LightCyan" BorderColor="LightCyan" Width="150px"
ForeColor="Black" />
<ItemStyle BackColor="LightCyan" BorderColor="LightCyan" />
<FooterStyle BackColor="LightCyan" BorderColor="LightCyan" Height="25px" />
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" HeaderText="Action" >
<HeaderStyle BackColor="LightCyan" BorderColor="LightCyan" Width="50px"
ForeColor="Black" />
<ItemStyle BackColor="White" BorderColor="LightCyan" />
<FooterStyle BackColor="LightCyan" BorderColor="LightCyan" Height="25px" />
</asp:CommandField>
</Columns>
</asp:GridView>
นี่เป็นส่วน vb ค่ะ
Sub loadgridview()
SQL(0) = " select Name_RefType , Rate_Refund , case when status = '1' then 'Use' else 'Not Use' end as statusStr from " _
& " ( select ID_RefType , Name_RefType , status from Refund_Type ) as t1 " _
& " left outer join ( select ID_RefType , Rate_Refund from Refund_Rate) as t2 " _
& " on t1.ID_RefType = t2.ID_RefType where status = '1'"
DT = M1.GetDatatable(SQL(0))
Try
ViewState("data") = DT
GridView1.DataSource = DT
GridView1.DataBind()
Catch ex As Exception
GridView1 = Nothing
End Try
End Sub
Sub modEditCommand(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
GridView1.EditIndex = e.NewEditIndex
GridView1.ShowFooter = False
loadgridview()
End Sub
Sub modUpdateCommand(ByVal s As Object, ByVal e As GridViewUpdateEventArgs)
'*** nametype ***'
Dim txtType As TextBox = GridView1.Rows(e.RowIndex).FindControl("name_rate")
'*** rate ***'
Dim txtRate As TextBox = GridView1.Rows(e.RowIndex).FindControl("rate")
'*** status ***'
Dim RadStatus As RadioButtonList = GridView1.Rows(e.RowIndex).FindControl("rdlStatusEdit")
Dim rateDouble As Double = 0
Double.TryParse(txtRate.Text, rateDouble)
'alert(e.RowIndex)
SQL(0) = " UPDATE Refund_Type SET Name_RefType = '" & txtType.Text & "' " _
& " , Updated_Date = getDate() , Updated_By = 'who' , status = '" & RadStatus.SelectedValue & "' " _
& " WHERE ID_RefType = " & (e.RowIndex + 1)
M1.Execute(SQL(0))
SQL(1) = "select * from Refund_Rate where ID_RefType = " & (e.RowIndex + 1)
DT1 = M1.GetDatatable(SQL(2))
SQL(2) = " UPDATE Refund_Rate SET Rate_Refund = '" & Round(rateDouble, 2) & "' , Created_Date = getDate() , " _
& "Created_By = 'who' , Status = '" & RadStatus.SelectedValue & "' Where ID_RefType = " & (e.RowIndex + 1)
M1.Execute(SQL(2))
'objCmd = New OleDbCommand(strSQL, objConn)
'objCmd.ExecuteNonQuery()
alert("ดำเนินการเรียบร้อยแล้วค่ะ")
GridView1.EditIndex = -1
GridView1.ShowFooter = True
loadgridview()
End Sub
Sub myGridView_RowCommand(ByVal source As Object, ByVal e As GridViewCommandEventArgs)
'*** nametype ***'
Dim txtType As TextBox = CType(GridView1.FooterRow.FindControl("txtAddType"), TextBox)
'*** rate ***'
Dim txtRate As TextBox = CType(GridView1.FooterRow.FindControl("txtAddrate"), TextBox)
'*** status ***'
Dim RadStatus As RadioButtonList = CType(GridView1.FooterRow.FindControl("rdlStatus"), RadioButtonList)
Dim rateDouble As Double = 0
Double.TryParse(txtRate.Text, rateDouble)
'alert(Round(rateDouble, 2))
If e.CommandName = "Add" Then
If chkexistaddbt() = False Then
Exit Sub
ElseIf chkinputaddbt() = False Then
Exit Sub
ElseIf chkIsNumberaddbt() = False Then
Exit Sub
End If
SQL(0) = " insert into Refund_Type " _
& " ( Name_RefType , Updated_Date , Updated_By , Status ) " _
& " values ( '" & txtType.Text & "' , getdate() , 'kam' , '" & RadStatus.SelectedValue & "') "
M1.Execute(SQL(0))
SQL(1) = "select * from Refund_Type order by ID_RefType DESC"
DT = M1.GetDatatable(SQL(1))
Dim id_type As String = DT.Rows(0)("ID_RefType")
Dim statusVar As String = DT.Rows(0)("Status")
SQL(2) = " insert into Refund_Rate " _
& " ( ID_RefType , Rate_Refund , Created_Date , Created_By , Status ) " _
& " values ( '" & id_type & "' , '" & Round(rateDouble, 2) & "' , getdate() , 'kam' , '" & statusVar & "') "
M1.Execute(SQL(2))
alert("ดำเนินการเรียบร้อยแล้วค่ะ")
loadgridview()
End If
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack() Then
loadgridview()
End If
End Sub
ขอบคุณอีกครั้งค่ะ ><
Tag : .NET, Ms SQL Server 2005, HTML/CSS, VB.NET
|
|
 |
 |
 |
 |
Date :
2013-04-05 14:19:16 |
By :
nuchkamol |
View :
1048 |
Reply :
1 |
|
 |
 |
 |
 |
|
|
|
 |