PDA

View Full Version : [CLOSED] combobox setrowvalue code behind does not work



jstifel
Jan 05, 2014, 10:25 PM
Hi, I run into a problem when trying to select default value for a combobox. I have binded items to combox by using a dataview as datasource and tried to set default value using setrowvalue function. I get the posible values for combobox listed back on client side but no one selected. I have checked value assigned with SetRowValue is there and of String type to make sure. Here goes code example:




<%@ Page Title="" Language="VB" MasterPageFile="~/site_no_ajax.master" AutoEventWireup="false" CodeFile="utmxmenu2.aspx.vb" Inherits="utmxmenu2" %>




<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">

<script type="text/jscript">
var refreshMenu = function (tree) {
App.direct.RefreshMenu({
success: function (result) {
var nodes = eval(result);
if (nodes.length > 0) {
tree.setRootNode(nodes[0]);
}
else {
tree.getRootNode().removeAll();
}
}
});
};
</script>


<extnet:ResourceManager ID="ResourceManager1" runat="server" />
<extnet:Hidden runat="server" ID="RecordId" />
<extnet:Hidden runat="server" ID="RecordIndex" />
<extnet:Viewport ID="Viewport1" runat="server" Layout="BorderLayout">
<Items>
<extnet:FormPanel runat="server"
ID="TopPanel"
Region="North"
Title="Informacion del Menu" DefaultAnchor="100%"
BodyPadding="5"
Collapsible="true">
<FieldDefaults LabelWidth="150" />
<Items>
<extnet:ComboBox
ID="menuId"
runat="server"
FieldLabel="Menu Seleccionado"
TypeAhead="true"
QueryMode="local"
ForceSelection="true"
EmptyText="Sin valor..."
ValueNotFoundText="Valor desconocido..."
Editable="false"
ValueField="SECURITY_IND"
DisplayField="DESC_TEXT">
<Store>
<extnet:Store runat="server">
<Model>
<extnet:Model ID="Model1" runat="server" IDProperty="SECURITY_IND">
<Fields>
<extnet:ModelField Name="SECURITY_IND" />
<extnet:ModelField Name="DESC_TEXT" />
</Fields>
</extnet:Model>
</Model>
</extnet:Store>
</Store>
<Listeners>
<Change Handler="refreshMenu(#{treeMenu});" />
</Listeners>
</extnet:ComboBox>
</Items>
</extnet:FormPanel>
<extnet:TreePanel ID="treeMenu" runat="server" Width="400" Icon="BookOpen" Title="Menu del Sistema"
AutoScroll="true" Split="true" Collapsible="false" Region="West"
RootVisible="true">
<Fields>
<extnet:ModelField Name="id" />
<extnet:ModelField Name="desc_text" />
<extnet:ModelField Name="run_text" />
<extnet:ModelField Name="param_text" />
</Fields>
<ColumnModel>
<Columns>
<extnet:TreeColumn runat="server" DataIndex="desc_text" Flex="1" Text="Descripcion"></extnet:TreeColumn>
<extnet:Column runat="server" DataIndex="run_text" Flex="1" Text="Comando"></extnet:Column>
<extnet:Column runat="server" DataIndex="param_text" Flex="1" Text="Parametros"></extnet:Column>
</Columns>
</ColumnModel>
<SelectionSubmitConfig Encode="True" />
<BottomBar>
<extnet:StatusBar ID="StatusBar1" runat="server" AutoClear="1500" />
</BottomBar>
<DirectEvents>
<ItemClick OnEvent="ItemSelected">
<ExtraParams>
<extnet:Parameter Name="id" Value="record.data.id" Mode="Raw" />
<extnet:Parameter Name="id1" Value="node.nodeid" Mode="Raw" />
<extnet:Parameter Name="index" Value="index" Mode="Raw" />
</ExtraParams>
</ItemClick>
<ItemMove OnEvent="ItemMove">
<ExtraParams>
<extnet:Parameter Name="item" Value="item.data.id" Mode="Raw" Encode="True" />
<extnet:Parameter Name="oldparent" Value="oldParent.data.id" Mode="Raw" Encode="True" />
<extnet:Parameter Name="newparent" Value="newParent.data.id" Mode="Raw" Encode="True" />
<extnet:Parameter Name="index" Value="index" Mode="Raw" />
</ExtraParams>
</ItemMove>
</DirectEvents>
<View>
<extnet:TreeView>
<Plugins>
<extnet:TreeViewDragDrop runat="server" ContainerScroll="false" AllowLeafDrop="True" AllowContainerDrops="True" />
</Plugins>
</extnet:TreeView>
</View>
<Plugins>
<extnet:CellEditing runat="server" />
</Plugins>
<Buttons>
<extnet:Button ID="Button2" runat="server" Text="Colapsar Menu" Icon="MagifierZoomOut">
<Listeners>
<Click Handler="#{treeMenu}.collapseAll();" />
</Listeners>
</extnet:Button>
<extnet:Button ID="Button1" runat="server" Text="Expandir Menu" Icon="MagnifierZoomIn">
<Listeners>
<Click Handler="#{treeMenu}.expandAll();" />
</Listeners>
</extnet:Button>
<extnet:Button ID="Button4" runat="server" Text="Refrescar" Icon="TableRefresh">
<Listeners>
<Click Handler="refreshMenu(#{treeMenu});" />
</Listeners>
</extnet:Button>
</Buttons>

</extnet:TreePanel>
<extnet:FormPanel runat="server"
ID="ItemFormPanel"
Region="Center"
Title="Informacion del Item del Menu" DefaultAnchor="100%"
BodyPadding="5">
<FieldDefaults LabelWidth="150" />
<Items>
<extnet:TextField runat="server" ID="fdesc_text" FieldLabel="Descripcion" />
<extnet:TextField runat="server" ID="frun_text" FieldLabel="Comando" />
<extnet:TextField runat="server" ID="fparam_text" FieldLabel="Parametros" />
<extnet:Checkbox runat="server" ID="fvisible_flag" FieldLabel="Visible segun perfil" />
</Items>
<Buttons>
<extnet:Button ID="newbtn" runat="server" Text="Nuevo" Icon="New">
<DirectEvents>
<Click OnEvent="ItemNew">
</Click>
</DirectEvents>
</extnet:Button>
<extnet:Button ID="updatebtn" runat="server" Text="Modificar" Icon="TableSave">
<DirectEvents>
<Click OnEvent="ItemUpdate">
</Click>
</DirectEvents>
</extnet:Button>
<extnet:Button ID="Button3" runat="server" Text="Borrar" Icon="TableDelete">
<DirectEvents>
<Click OnEvent="ItemDelete">
</Click>
</DirectEvents>
</extnet:Button>
</Buttons>

</extnet:FormPanel>
</Items>
</extnet:Viewport>
</asp:Content>



and server side VB code:




Imports System.Collections.Generic
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data

Imports Psa.Data
Imports Psa.Session
Imports Psa.Files
Imports Psa.Application
Imports Psa.Convert
Imports Psa.Configuration
Imports Psa.Errors
Imports Psa.AMath

Imports Ext.Net.Utilities
Imports Ext.Net

Partial Class utmxmenu2
Inherits BasicPage

Private menu As Ext.Net.TreePanel
Private RootMenuItem As Ext.Net.Node
Private RootItem As Ext.Net.Node

Private Function ProfileId() As String
Return PsaConvert.ObjToInt(menuId.SelectedItem.Value).ToS tring
End Function

Protected Overloads Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
menu = treeMenu
If Not IsPostBack And Not Ext.Net.X.IsAjaxRequest Then
LoadMenus()
buildMenu(menu.Root)
End If
End Sub

Private Sub LoadMenus()
menuId.SetValue(Nothing)
menuId.Items.Clear()
Dim sql As String
sql = "select Rtrim(Str(SECURITY_IND)) SECURITY_IND, DESC_TEXT from userprofdesc"
Dim dv As System.Data.DataView = PsaData.CreateDataView(sql)
Dim s As Ext.Net.Store
s = menuId.Store(0)
s.DataSource = dv
s.DataBind()
If dv.Count > 0 Then
menuId.SetRawValue(PsaConvert.ObjToString(dv(0)("SECURITY_IND")))
End If
sql = ""
'Ext.Net.X.Js.Call("function", Nothing)
End Sub

Private Function buildMenu(ByVal nodes As Ext.Net.NodeCollection) As Ext.Net.NodeCollection
If nodes Is Nothing Then
nodes = New Ext.Net.NodeCollection
End If
RootMenuItem = New Ext.Net.Node
RootMenuItem.Text = "Home"
RootMenuItem.Icon = Icon.House
RootMenuItem.CustomAttributes.Add(New Ext.Net.ConfigItem With {.Name = "id", .Value = "-1"})
nodes.Add(RootMenuItem)
PsaData.CreateConnection(PsaSession.ConnectionStri ng())
Dim sql As String
Dim dr As DataRow = Nothing
sql = "select ParentId,Id,Desc_Text,run_text,param_text from menus where profileid = %1"
sql = sql.Replace("%1", ProfileId)
Dim ds As DataSet = PsaData.CreateDataSet(sql)

Dim dbRow As System.Data.DataRow
For Each dbRow In ds.Tables(0).Rows
If dbRow("ParentId").ToString = "0" Then
dbRow("ParentId") = System.DBNull.Value
End If
Next
ds.Relations.Add("NodeRelation", ds.Tables(0).Columns("Id"), ds.Tables(0).Columns("ParentId"))

For Each dbRow In ds.Tables(0).Rows
If (dbRow.IsNull("ParentId")) Then
RootItem = CreateItem(dbRow)
RootMenuItem.Children.Add(RootItem)
PopulateSubMenu(dbRow, RootItem)
RootItem.Icon = Icon.Folder
If RootItem.Children.Count = 0 Then
RootItem.Leaf = True
RootItem.Icon = Icon.Application
End If
End If
Next dbRow
' menu.CollapseAll()
RootMenuItem.Expanded = True
Return nodes
End Function

Private Sub PopulateSubMenu(ByVal dbRow As System.Data.DataRow, ByVal item As Ext.Net.Node)
Dim childRow As System.Data.DataRow
For Each childRow In dbRow.GetChildRows("NodeRelation")
Dim childItem As Ext.Net.Node = CreateItem(childRow)
item.Children.Add(childItem)
PopulateSubMenu(childRow, childItem)
If childItem.Children.Count = 0 Then
childItem.Leaf = True
childItem.Icon = Icon.Application
End If
Next childRow
End Sub

Private Function CreateItem(ByVal dbRow As System.Data.DataRow) As Ext.Net.Node
Dim item As New Ext.Net.Node()
item.NodeID = dbRow("id").ToString()
item.Text = PsaConvert.ObjToString(dbRow("desc_text"))
item.CustomAttributes.Add(New Ext.Net.ConfigItem() With {.Name = "id", .Value = dbRow("id").ToString()})
item.CustomAttributes.Add(New Ext.Net.ConfigItem() With {.Name = "desc_text", .Value = dbRow("desc_text").ToString()})
item.CustomAttributes.Add(New Ext.Net.ConfigItem() With {.Name = "run_text", .Value = dbRow("run_text").ToString()})
item.CustomAttributes.Add(New Ext.Net.ConfigItem() With {.Name = "param_text", .Value = dbRow("param_text").ToString()})
Return item
End Function

<DirectMethod> _
Public Function RefreshMenu() As String
Dim nodes As Ext.Net.NodeCollection = buildMenu(menu.Root)
Return nodes.ToJson
End Function

Protected Sub ItemSelected(sender As Object, e As DirectEventArgs)
' Notification.Show(New NotificationConfig With {.Title = "Item", .Html = e.ExtraParams("id") + "</br>" + e.ExtraParams("index")})
'Exit Sub
Try
Dim sql As String = "select * from menus where profileId = %1 and Id = %2"
sql = sql.Replace("%1", ProfileId)
sql = sql.Replace("%2", e.ExtraParams("id"))
Dim dr As DataRow = PsaData.CreateDataRow(sql)
RecordIndex.Text = e.ExtraParams("index")
RecordId.Text = e.ExtraParams("id")
fdesc_text.Text = ""
frun_text.Text = ""
fparam_text.Text = ""
If dr IsNot Nothing Then
fdesc_text.Text = PsaConvert.ObjToString(dr("desc_text"))
frun_text.Text = PsaConvert.ObjToString(dr("run_text"))
fparam_text.Text = PsaConvert.ObjToString(dr("param_text"))
End If
Catch ex As Exception
e.ErrorMessage = ex.Message
e.Success = False
End Try
End Sub

Protected Sub ItemMove(sender As Object, e As DirectEventArgs)
Dim i As Integer = 1
End Sub

Protected Sub ItemUpdate(sender As Object, e As DirectEventArgs)
Try
If fdesc_text.Text = "" Then
Throw New Exception("Descripcion es requerida")
End If
Dim sql As String
Dim dr As DataRow
sql = "select * from menus where profileId = %1 and Id = %2"
sql = sql.Replace("%1", ProfileId)
sql = sql.Replace("%2", RecordId.Text)
dr = PsaData.CreateDataRow(sql)
dr("desc_text") = fdesc_text.Text
dr("run_text") = frun_text.Text
dr("param_text") = fparam_text.Text
PsaData.UpdateTable(dr.Table, "select * from menus where 1=0")
Dim node As NodeProxy = treeMenu.GetNodeById(RecordId.Text)
If node IsNot Nothing Then
node.SetText(fdesc_text.Text)
node.Set("desc_text", fdesc_text.Text)
node.Set("run_text", frun_text.Text)
node.Set("param_text", fparam_text.Text)
End If
Notification.Show(New NotificationConfig With {.Title = "Item", .Html = "Item actualizado"})
Catch ex As Exception
e.ErrorMessage = ex.Message
e.Success = False
End Try
End Sub

Protected Sub ItemDelete(sender As Object, e As DirectEventArgs)
Try
Dim Sql = PsaFiles.ReadTextFile("~/sql/utmxmenu/sql2.sql").ToUpper
Sql = Sql.Replace(":PROFILE_ID", ProfileId)
Sql = Sql.Replace(":ID", RecordId.Text)
PsaData.ExecuteNonQuery(Sql)
fdesc_text.Text = "Eliminado"
Dim node As NodeProxy = treeMenu.GetNodeById(RecordId.Text)
If node IsNot Nothing Then
node.Remove()
Notification.Show(New NotificationConfig With {.Title = "Item", .Html = "Item eliminado"})
End If
Catch ex As Exception
e.ErrorMessage = ex.Message
e.Success = False
End Try
End Sub


Protected Sub ItemNew(sender As Object, e As DirectEventArgs)
Try
Dim sql As String
Dim dr As DataRow
sql = "select * from menus where 1=0"
Dim dt As DataTable = PsaData.CreateDataTable(sql)
dr = dt.NewRow
dt.Rows.Add(dr)
sql = "select max(id) from menus where profileId = %1"
sql = sql.Replace("%1", ProfileId)
Dim id As Integer = PsaConvert.ObjToInt(PsaData.ExecuteScalar(sql)) + 1
dr("profileId") = ProfileId()
dr("id") = id
dr("desc_text") = "Nuevo registro"
PsaData.UpdateTable(dr.Table, "select * from menus where 1=0")
Dim menuItem As Ext.Net.Node = CreateItem(dr)
Dim node As NodeProxy = treeMenu.GetNodeById(RecordId.Text)
If node IsNot Nothing Then
node.AppendChild(menuItem)
Else
End If
Notification.Show(New NotificationConfig With {.Title = "Item", .Html = "Item creado"})
Catch ex As Exception
e.ErrorMessage = ex.Message
e.Success = False
End Try
End Sub

End Class



As you can see, function LoadMenus reads data from database and assign it to combobox. I also checks if dataview has records and if so set the first one as default value.

Any hint?

Daniil
Jan 06, 2014, 2:16 PM
Hi @jstifel,

To set up an initial value, please use a ComboBox's SelectedItems.

Example

<%@ Page Language="C#" %>

<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>

<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!X.IsAjaxRequest)
{
Store store = this.ComboBox1.GetStore();
store.DataSource = new object[]
{
new object[] { "1", "Item 1" },
new object[] { "2", "Item 2" },
new object[] { "3", "Item 3" }
};

this.ComboBox1.SelectedItems.Add(new Ext.Net.ListItem() { Value = "1" });
}
}
</script>

<!DOCTYPE html>

<html>
<head runat="server">
<title>Ext.NET v2 Example</title>
</head>
<body>
<form runat="server">
<ext:ResourceManager runat="server" />
<ext:ComboBox
ID="ComboBox1"
runat="server"
DisplayField="text"
ValueField="value">
<Store>
<ext:Store runat="server">
<Model>
<ext:Model runat="server">
<Fields>
<ext:ModelField Name="value" />
<ext:ModelField Name="text" />
</Fields>
</ext:Model>
</Model>
<Reader>
<ext:ArrayReader />
</Reader>
</ext:Store>
</Store>
</ext:ComboBox>
</form>
</body>
</html>

jstifel
Jan 06, 2014, 5:32 PM
OK, the tip does the job with a couple of exceptions, I do no want to trigger the onchange event of combobox the first time when page is loaded. That is why I was using SetRawValue and not SetValue. Is is there any way to do it using the way you suggest?

Also, when calling server side RefreshMenu from javascript, if I evaluate selectedItem.value I'm always getting the description for the selected item and not the value. I know I have setup valuefield and displayfield correctly.

Daniil
Jan 07, 2014, 4:32 AM
OK, the tip does the job with a couple of exceptions, I do no want to trigger the onchange event of combobox the first time when page is loaded. That is why I was using SetRawValue and not SetValue. Is is there any way to do it using the way you suggest?


Please set up FireChangeOnLoad="false" for the ComboBox.



Also, when calling server side RefreshMenu from javascript, if I evaluate selectedItem.value I'm always getting the description for the selected item and not the value. I know I have setup valuefield and displayfield correctly.

Please change my example to get that issue reproducible.