Hello programmers, got a few twitching i need to resolve here.
I am creating an excel like updating wherein if the user click on a specific column/row it will change to textbox and when he click it another it will return to label but it is already been updated. By the way I am using usercontrol that will bi bind based on the row of a gridview
My codes are as follows:
clientside
<%@ Control Language="VB" AutoEventWireup="false" CodeFile="CourseUserControl.ascx.vb"
Inherits="CourseUserControl" %>
<asp:GridView ID="grdCourse" runat="server" Width="800px" HorizontalAlign="Left"
AutoGenerateColumns="False" align="center" DataKeyNames="TrainingPlanID" Font-Names="verdana,arial,helvetica,sans-serif"
Font-Size="8pt" BackColor="White" GridLines="Vertical" OnRowCommand="grdCourse_RowCommand">
<Columns>
<asp:ButtonField Text="SingleClick" CommandName="SingleClick" Visible="False" />
<asp:TemplateField HeaderText="TrainingPlanID" SortExpression="TrainingPlanID" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="lblTrainingPlanID" runat="server" Text='<%# Eval("TrainingPlanID")%>'
Width="30px" Height="10px"> </asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CourseName" SortExpression="CourseName" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="lblCoursename" runat="server" Text='<%# Bind("CourseName")%>' Width="300px"
Height="10px"> </asp:Label>
<asp:TextBox ID="txtCourseName" runat="server" Text='<%# Bind("CourseName") %>' Font-Size="XX-Small"
Font-Names="Verdana" Width="300px" Visible="false"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Course Code" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="lblCourseCode" runat="server" Text='<%# Eval("CourseCode")%>' Height="10px"> </asp:Label>
<asp:TextBox ID="txtCourseCode" runat="server" Text='<%# Eval("CourseCode") %>' Font-Size="XX-Small"
Font-Names="Verdana" Width="300px" Visible="false"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Duration" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="lblDuration" runat="server" Text='<%# Eval("Duration")%>' Height="10px"> </asp:Label>
<asp:TextBox ID="txtDuration" runat="server" Text='<%# Eval("Duration")%>' Font-Size="XX-Small"
Font-Names="Verdana" Visible="false"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EditDetail" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:ImageButton ID="EditDetail" Height="15px" ImageUrl="Images/edit2.png" runat="server"
CommandName="cmdlink" CommandArgument='<%# Eval("TrainingPlanID")%>' PostBackUrl='<%#"TrainingPlanDetails.aspx?TPID=" & Eval("TrainingPlanID")%>' />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:ImageButton ID="ImageDeleteCourse" Height="15px" ImageUrl="Images/Delete.png"
runat="server" CommandName="cmdDelete" CommandArgument='<%# Eval("TrainingPlanID")%>'
OnClick="ImageDeleteCourse_Click" OnClientClick="return confirm('Are you sure to delete this Course?')" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True" />
<asp:TemplateField HeaderText="DisciplineID" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="DisciplineCourseID" runat="server" Text='<%# Eval("DisciplineID")%>'
Height="10px"> </asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EditCourse" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:ImageButton ID="ImageEdit" Height="15px" ImageUrl="Images/edit.png" runat="server"
CommandName="cmdlink" CommandArgument='<%# Eval("TrainingPlanID")%>' />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:CommandField ShowEditButton="true" />
</Columns>
<RowStyle BackColor="White" ForeColor="#333333" />
<FooterStyle BackColor="White" ForeColor="#333333" />
<PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
</asp:GridView>
this is the client side now on my usercontrol gridview updating here is my code
Protected Sub grdCourse_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles grdCourse.RowUpdating
Dim _gridView As GridView = DirectCast(sender, GridView)
Dim key As String = ""
Dim value As String = ""
Dim _columnKeys As String() = New String() {"CourseName", "CourseCode", "Duration"}
If e.RowIndex > -1 Then
For i As Integer = _firstEditCellIndex To 4
Dim _displayControl As Control = _gridView.Rows(e.RowIndex).Cells(i).Controls(1)
Dim _editControl As Control = _gridView.Rows(e.RowIndex).Cells(i).Controls(3)
key = _columnKeys(i - _firstEditCellIndex)
If _editControl.Visible Then
If TypeOf _editControl Is TextBox Then
value = DirectCast(_editControl, TextBox).Text
End If
e.NewValues.Add(key, value)
Else
value = DirectCast(_displayControl, Label).Text.ToString()
e.NewValues.Add(key, value)
End If
Next
_gridView.SelectedIndex = -1
End If
End Sub
as you see that is my error the value doesn't give what i typed in. It is null
any ideas?
Update:
My bad. My goal si this;
Switching from label to textbox
when I clicked on label it will change to textbox with the text of the label on the textbox
now when I change the content of the textbox that is the problem cause even if I have a text on my textbox
If TypeOf _editControl Is TextBox Then<br />
value = DirectCast(_editControl, TextBox).Text
the value is Null or "" which mean i cant get the text
By the way here is my code on rowcommand and rowdatabound so that you could understand what I am doing
Protected Sub grdCourse_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles grdCourse.RowCommand
Dim _gridView As GridView = DirectCast(sender, GridView)
Select Case e.CommandName
Case ("SingleClick")
Dim _rowIndex As Integer = Integer.Parse(e.CommandArgument.ToString())
Dim _columnIndex As Integer = Integer.Parse(Request.Form("__EVENTARGUMENT"))
_gridView.SelectedIndex = _rowIndex
CourseBindGrid()
Dim _displayControl As Control = _gridView.Rows(_rowIndex).Cells(_columnIndex).Controls(1)
_displayControl.Visible = False
Dim _editControl As Control = _gridView.Rows(_rowIndex).Cells(_columnIndex).Controls(3)
_editControl.Visible = True
_gridView.Rows(_rowIndex).Cells(_columnIndex).Attributes.Clear()
ScriptManager.RegisterStartupScript(Me, [GetType](), "SetFocus", "document.getElementById('" + _editControl.ClientID + "').focus();", True)
If TypeOf _editControl Is TextBox Then
DirectCast(_editControl, TextBox).Attributes.Add("onfocus", "this.select()")
End If
Exit Select
End Select
End Sub
Protected Sub grdCourse_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdCourse.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim disID As Label = DirectCast(e.Row.FindControl("DisciplineCourseID"), Label)
Dim myImage As ImageButton = DirectCast(e.Row.FindControl("ImageDeleteCourse"), ImageButton)
If disID.Text = "" Then
myImage.Visible = False
Else
myImage.Visible = True
Dim _singleClickButton As LinkButton = DirectCast(e.Row.Cells(0).Controls(0), LinkButton)
Dim _jsSingle As String = Page.ClientScript.GetPostBackClientHyperlink(_singleClickButton, "")
If Page.Validators.Count > 0 Then
_jsSingle = _jsSingle.Insert(11, "if(Page_ClientValidate())")
End If
For columnIndex As Integer = _firstEditCellIndex To 5 - 1
Dim js As String = _jsSingle.Insert(_jsSingle.Length - 2, columnIndex.ToString())
e.Row.Cells(columnIndex).Attributes("onclick") = js
e.Row.Cells(columnIndex).Attributes("style") += "cursor:pointer;cursor:hand;"
Next
End If
End If
End Sub