Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems caused by postback

Status
Not open for further replies.

lpatnaik

Programmer
Jul 11, 2002
53
Hi,
I have a page which apart from having some text and dropdown fields has an file upload button and a submit button.
The problem is
1. when I upload a file the control first goes to the "if page.postback" case in the "page load" and then comes to the function i have specified to upload. I have something else in the postback scenario(submitting the page which should update database)
2. when i change a select box, the onselectedindex event fires, but before that it goes to the page load as above.
3. on submitting the form, the page load is fired as above.


Now, I have used a hidden field to trap these occurences but in any case, i cannot trap that in the page load.
I am giving my code below. Please give me some advice on this.

CODE:
Imports projectcentral.isdevsrv1
Imports Admin

Public Class new_testcase
Inherits System.Web.UI.Page

Const L_TESTCASE = "TC"
Const L_PROJECTCHANGE = 1
Const L_UPLOAD = 2
Const L_SUBMIT = 3

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region
Protected WithEvents selProject As System.Web.UI.WebControls.DropDownList
Protected WithEvents selModule As System.Web.UI.WebControls.DropDownList
Protected WithEvents selRequirement As System.Web.UI.WebControls.DropDownList
Protected WithEvents selTestType As System.Web.UI.WebControls.DropDownList
Protected WithEvents txtTitle As System.Web.UI.WebControls.TextBox
Protected WithEvents txtAuthor As System.Web.UI.WebControls.TextBox
Protected WithEvents txtDescription As System.Web.UI.WebControls.TextBox
Protected WithEvents txtExpectedResult As System.Web.UI.WebControls.TextBox
Protected WithEvents txtAuthorComments As System.Web.UI.WebControls.TextBox
Protected WithEvents divUserRole As System.Web.UI.HtmlControls.HtmlGenericControl
Protected WithEvents divError As System.Web.UI.HtmlControls.HtmlGenericControl
Protected WithEvents btnUpload As System.Web.UI.WebControls.Button
Protected WithEvents btnSave As System.Web.UI.WebControls.Button
Protected WithEvents fileTestScript As System.Web.UI.HtmlControls.HtmlInputFile
Protected WithEvents divCustomerAccess As System.Web.UI.HtmlControls.HtmlGenericControl
Protected WithEvents chkCustomerAccess As System.Web.UI.WebControls.CheckBox
Protected WithEvents chkUserRole As System.Web.UI.WebControls.CheckBoxList
Protected WithEvents divSub As System.Web.UI.HtmlControls.HtmlGenericControl
Protected WithEvents hRoles As System.Web.UI.HtmlControls.HtmlInputHidden
Protected WithEvents selPlatform As System.Web.UI.WebControls.DropDownList
Protected WithEvents hPageValue As System.Web.UI.HtmlControls.HtmlInputHidden


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim strProjectID As String
If Page.IsPostBack And hPageValue.Value = L_SUBMIT Then
Dim objTCservice As New wsTestCase()
Dim TestCase As New TestCaseDO()
Dim arrUserRoles(2) As String
Dim ReturnDO As New ReturnedDO()


Dim intTotalChk As Integer = hRoles.Value
If intTotalChk > 0 Then
Dim i, j As Integer
i = 0

For j = 0 To intTotalChk - 1
If (Request.Form(&quot;chkUserRole&quot; & (j + 1)) <> &quot;&quot;) Then
ReDim Preserve arrUserRoles(i)
arrUserRoles(i) = Request.Form(&quot;chkUserRole&quot; & (j + 1))
i = i + 1
End If
Next
End If

TestCase.ProjectID = selProject.SelectedItem.Value
TestCase.Title = txtTitle.Text
TestCase.Author = txtAuthor.Text
TestCase.Description = txtDescription.Text
TestCase.ExpectedResult = txtExpectedResult.Text
TestCase.ModuleID = selModule.SelectedItem.Value
TestCase.RequirementID = selRequirement.SelectedItem.Value
If Not (selPlatform.SelectedIndex = 0) Then
TestCase.PlatformID = selPlatform.SelectedItem.Value
End If
If Not (fileTestScript.PostedFile Is Nothing) Then
TestCase.TestScript = fileTestScript.PostedFile.FileName
End If

TestCase.TestTypeID = selTestType.SelectedItem.Value
TestCase.AuthorComments = txtAuthorComments.Text
TestCase.DateCreated = Date.Now
TestCase.UserRoles = arrUserRoles
If (chkCustomerAccess.Enabled = True) Then
TestCase.ShowToCustomer = chkCustomerAccess.Checked
End If
ReturnDO = objTCservice.Add(TestCase)

If ReturnDO.Success = False Then
divError.InnerHtml = ReturnDO.Message
divError.Style.Item(&quot;display&quot;) = &quot;block&quot;
Else
Response.Redirect(&quot;list_testcase.aspx&quot;)
End If
Else
'strProjectID = 4 'Session(&quot;strProjectID&quot;)
End If

If hPageValue.Value <> L_PROJECTCHANGE Or hPageValue.Value <> L_UPLOAD Or hPageValue.Value <> L_SUBMIT Then
GetFullName()
InitLookUpFields(4)
End If

If hPageValue.Value = L_UPLOAD Then
' strProjectID = selProject.SelectedItem.Value
End If
End Sub

Sub GetFullName()
Dim strUserID, strUserName As String
strUserID = (Right(Request.ServerVariables(&quot;AUTH_USER&quot;), Len(Request.ServerVariables(&quot;AUTH_USER&quot;)) - InStr(1, Request.ServerVariables(&quot;AUTH_USER&quot;), &quot;\&quot;)))
Try
Dim objAdmin As New Admin.UserAdmin()
strUserName = objAdmin.getFullName(strUserID)
objAdmin = Nothing
txtAuthor.Text = strUserName
Catch objError As Exception
divError.InnerHtml = &quot;Error: &quot; & objError.Message.ToString()
divError.Style.Item(&quot;display&quot;) = &quot;block&quot;
End Try
End Sub

Sub InitLookUpFields(ByVal strProjectID As String)
Dim strUserID, strProjectName As String
Dim strProjectSql, strModuleSql, strUserSql, strReqSql, strTestTypeSql, strCustomerViewSql As String
strUserID = (Right(Request.ServerVariables(&quot;AUTH_USER&quot;), Len(Request.ServerVariables(&quot;AUTH_USER&quot;)) - InStr(1, Request.ServerVariables(&quot;AUTH_USER&quot;), &quot;\&quot;)))
Dim objDAL As New DAL()
objDAL.ConnectionString = ConfigurationSettings.AppSettings(&quot;strConnectionString&quot;)

Try
Dim dsResult As DataSet
Dim arrParams(1) As DBParameterDO
arrParams(0) = New DBParameterDO(&quot;@LoginID&quot;, SqlDbType.VarChar, 100, strUserID)
dsResult = objDAL.RunSPReturnDataset(&quot;sp_GetUserProjects&quot;, arrParams)
selProject.DataSource = dsResult
selProject.DataValueField = &quot;ProjectID&quot;
selProject.DataTextField = &quot;ProjectName&quot;
selProject.DataBind()
selProject.Items.FindByValue(&quot;4&quot;).Selected = True

ReDim arrParams(2)
arrParams(0) = New DBParameterDO(&quot;@projectID&quot;, SqlDbType.VarChar, 100, strProjectID)
arrParams(1) = New DBParameterDO(&quot;@tablename&quot;, SqlDbType.VarChar, 100, &quot;tblProjectModule&quot;)
dsResult = objDAL.RunSPReturnDataset(&quot;sp_GetProjectAttributes&quot;, arrParams)
selModule.DataSource = dsResult
selModule.DataValueField = &quot;ModuleID&quot;
selModule.DataTextField = &quot;ModuleName&quot;
selModule.DataBind()
selModule.Items.Insert(0, &quot;-- Select a module --&quot;)

arrParams(0) = New DBParameterDO(&quot;@projectID&quot;, SqlDbType.VarChar, 100, strProjectID)
arrParams(1) = New DBParameterDO(&quot;@tablename&quot;, SqlDbType.VarChar, 100, &quot;tblProjectUserRoles&quot;)
dsResult = objDAL.RunSPReturnDataset(&quot;sp_GetProjectAttributes&quot;, arrParams)

Dim intTotRoles As Integer = dsResult.Tables(0).Rows.Count
Dim intLpCnt As Integer
Dim strRole As String
If intTotRoles > 0 Then
strRole = &quot;<table><tr>&quot;
End If
For intLpCnt = 0 To intTotRoles - 1
strRole = strRole & &quot;<td><input type=checkbox id=chkUserRole&quot; & (intLpCnt + 1) & &quot; name=chkUserRole&quot; & (intLpCnt + 1) & &quot; value='&quot; & dsResult.Tables(0).Rows(intLpCnt)(&quot;ProjectRoleID&quot;).ToString() & &quot;'></td>&quot;
strRole = strRole & &quot;<td class=capstyle align=top>&quot; & dsResult.Tables(0).Rows(intLpCnt)(&quot;UserRole&quot;).ToString() & &quot;</td>&quot;
If ((intLpCnt / 3) = 1 And intLpCnt <> (intTotRoles - 1)) Then
strRole = strRole & &quot;</tr><tr>&quot;
End If
Next
strRole = strRole & &quot;</tr></table>&quot;
divSub.InnerHtml = strRole
divSub.Style.Item(&quot;display&quot;) = &quot;block&quot;
hRoles.Value = intTotRoles
divUserRole.Style.Item(&quot;display&quot;) = &quot;block&quot;
'chkUserRole.DataSource = dsResult
'chkUserRole.DataTextField = &quot;UserRole&quot;
'chkUserRole.DataValueField = &quot;ProjectRoleID&quot;
'chkUserRole.DataBind()


arrParams(0) = New DBParameterDO(&quot;@projectID&quot;, SqlDbType.VarChar, 100, strProjectID)
arrParams(1) = New DBParameterDO(&quot;@tablename&quot;, SqlDbType.VarChar, 100, &quot;tblProjectRequirements&quot;)
dsResult = objDAL.RunSPReturnDataset(&quot;sp_GetProjectAttributes&quot;, arrParams)
selRequirement.DataSource = dsResult
selRequirement.DataValueField = &quot;RequirementID&quot;
selRequirement.DataTextField = &quot;Title&quot;
selRequirement.DataBind()
selRequirement.Items.Insert(0, &quot;-- Select a requirement --&quot;)

arrParams(1) = Nothing
ReDim arrParams(1)
arrParams(0) = New DBParameterDO(&quot;@tableName&quot;, SqlDbType.VarChar, 100, &quot;tblTestType&quot;)
dsResult = objDAL.RunSPReturnDataset(&quot;sp_GetMasterDetails&quot;, arrParams)
selTestType.DataSource = dsResult
selTestType.DataValueField = &quot;ID&quot;
selTestType.DataTextField = &quot;Name&quot;
selTestType.DataBind()
selTestType.Items.Insert(0, &quot;-- Select a test type --&quot;)

arrParams(1) = Nothing
ReDim arrParams(1)
arrParams(0) = New DBParameterDO(&quot;@tableName&quot;, SqlDbType.VarChar, 100, &quot;tblPlatform&quot;)
dsResult = objDAL.RunSPReturnDataset(&quot;sp_GetMasterDetails&quot;, arrParams)
selPlatform.DataSource = dsResult
selPlatform.DataValueField = &quot;ID&quot;
selPlatform.DataTextField = &quot;Name&quot;
selPlatform.DataBind()
selPlatform.Items.Insert(0, &quot;-- Select a platform --&quot;)

Dim intReturn As Integer
ReDim arrParams(2)
arrParams(0) = New DBParameterDO(&quot;@projectID&quot;, SqlDbType.VarChar, 100, strProjectID)
arrParams(1) = New DBParameterDO(&quot;@checkType&quot;, SqlDbType.VarChar, 2, L_TESTCASE)
intReturn = objDAL.RunSPReturnInteger(&quot;sp_GetCustomerAccess&quot;, arrParams)
If intReturn = 1 Then
chkCustomerAccess.Enabled = True
divCustomerAccess.Style.Item(&quot;display&quot;) = &quot;block&quot;
Else
chkCustomerAccess.Enabled = False
End If

Catch objError As DBException
divError.InnerHtml = objError.Message.ToString()
divError.Style.Item(&quot;display&quot;) = &quot;block&quot;
Catch objError As Exception
divError.InnerHtml = &quot;Error: &quot; & objError.Message.ToString()
divError.Style.Item(&quot;display&quot;) = &quot;block&quot;
Finally
objDAL = Nothing
End Try
End Sub

Public Sub GetProjectAttributes(ByVal sender As System.Object, ByVal objArgs As System.EventArgs)
Dim strUserID, strUserName, strProjectName As String
Dim strProjectSql, strModuleSql, strUserSql, strReqSql, strTestTypeSql, strCustomerViewSql As String
Dim strProjectID As String = selProject.SelectedItem.Value 'Session(&quot;strProjectID&quot;)

hPageValue.Value = L_PROJECTCHANGE

Dim objDAL As New DAL()
objDAL.ConnectionString = ConfigurationSettings.AppSettings(&quot;strConnectionString&quot;)

Dim dsResult As DataSet
Dim arrParams(2) As DBParameterDO

arrParams(0) = New DBParameterDO(&quot;@projectID&quot;, SqlDbType.VarChar, 100, strProjectID)
arrParams(1) = New DBParameterDO(&quot;@tablename&quot;, SqlDbType.VarChar, 100, &quot;tblProjectModule&quot;)
dsResult = objDAL.RunSPReturnDataset(&quot;sp_GetProjectAttributes&quot;, arrParams)
selModule.DataSource = dsResult
selModule.DataValueField = &quot;ModuleID&quot;
selModule.DataTextField = &quot;ModuleName&quot;
selModule.DataBind()
selModule.Items.Insert(0, &quot;-- Select a module --&quot;)
Response.Write(strProjectID)
Response.Write(&quot;,&quot;)
Response.Write(selModule.Items(1).Value)
Response.End()

arrParams(0) = New DBParameterDO(&quot;@projectID&quot;, SqlDbType.VarChar, 100, strProjectID)
arrParams(1) = New DBParameterDO(&quot;@tablename&quot;, SqlDbType.VarChar, 100, &quot;tblProjectUserRoles&quot;)
dsResult = objDAL.RunSPReturnDataset(&quot;sp_GetProjectAttributes&quot;, arrParams)

Dim intTotRoles As Integer = dsResult.Tables(0).Rows.Count
Dim intLpCnt As Integer
Dim strRole As String
If intTotRoles > 0 Then
strRole = &quot;<table><tr>&quot;
End If
For intLpCnt = 0 To intTotRoles - 1
strRole = strRole & &quot;<td><input type=checkbox id=chkUserRole&quot; & (intLpCnt + 1) & &quot; name=chkUserRole&quot; & (intLpCnt + 1) & &quot; value='&quot; & dsResult.Tables(0).Rows(intLpCnt)(&quot;ProjectRoleID&quot;).ToString() & &quot;'></td>&quot;
strRole = strRole & &quot;<td class=capstyle align=top>&quot; & dsResult.Tables(0).Rows(intLpCnt)(&quot;UserRole&quot;).ToString() & &quot;</td>&quot;
If ((intLpCnt / 3) = 1 And intLpCnt <> (intTotRoles - 1)) Then
strRole = strRole & &quot;</tr><tr>&quot;
End If
Next
strRole = strRole & &quot;</tr></table>&quot;
divSub.InnerHtml = strRole
divSub.Style.Item(&quot;display&quot;) = &quot;block&quot;
hRoles.Value = intTotRoles
divUserRole.Style.Item(&quot;display&quot;) = &quot;block&quot;

arrParams(0) = New DBParameterDO(&quot;@projectID&quot;, SqlDbType.VarChar, 100, strProjectID)
arrParams(1) = New DBParameterDO(&quot;@tablename&quot;, SqlDbType.VarChar, 100, &quot;tblProjectRequirements&quot;)
dsResult = objDAL.RunSPReturnDataset(&quot;sp_GetProjectAttributes&quot;, arrParams)
selRequirement.DataSource = dsResult
selRequirement.DataValueField = &quot;RequirementID&quot;
selRequirement.DataTextField = &quot;Title&quot;
selRequirement.DataBind()
selRequirement.Items.Insert(0, &quot;-- Select a requirement --&quot;)

Dim intReturn As Integer
arrParams(0) = New DBParameterDO(&quot;@projectID&quot;, SqlDbType.VarChar, 100, strProjectID)
arrParams(1) = New DBParameterDO(&quot;@checkType&quot;, SqlDbType.VarChar, 2, L_TESTCASE)
intReturn = objDAL.RunSPReturnInteger(&quot;sp_GetCustomerAccess&quot;, arrParams)
If intReturn = 1 Then
chkCustomerAccess.Enabled = True
divCustomerAccess.Style.Item(&quot;display&quot;) = &quot;block&quot;
Else
chkCustomerAccess.Enabled = False
End If
End Sub

Private Sub btnUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpload.Click
Dim filename As String
hPageValue.Value = L_UPLOAD

If Not (fileTestScript.PostedFile Is Nothing) Then
Try
fileTestScript.PostedFile.SaveAs(&quot;c:\MyUploadFile.gif&quot;)
Catch objError As Exception
divError.InnerHtml = &quot;File could not be uploaded&quot;
divError.Style.Item(&quot;display&quot;) = &quot;block&quot;
End Try
Else
divError.Style.Item(&quot;display&quot;) = &quot;block&quot;
End If
End Sub


End Class
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top