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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Handling the inserted event of the objectdatasource problems

Status
Not open for further replies.

jgd123456

Programmer
Nov 3, 2006
19
GB
Hi, i'm trying to do an insert using the objectdatasource control to call my method in the bll. The problem i have is handling the return type. In my code behind i have the following:

Code:
protected void objUser_Inserted(object sender, System.Web.UI.WebControls.ObjectDataSourceStatusEventArgs e) {
    if ((bool) e.ReturnValue == true) {
        Response.Redirect("~/Admin/Documents/");
    } else {
        Label lblSummary = (Label) fvwUser.FindControl("lblSummary");
        lblSummary.Text = "Error: Username already exists";
        lblSummary.Visible = true;
    }
}

This works fine if the return value is true (it redirects back to the documents folder in the admin area). However when i encounter a problem the contents of the lblSummary are not updated. By default i set its visible property to false and then hope to make it visible but it will not show. Appreciate if someone could tell me what i am doing wrong. Thanks
 
Have you stepped through the code? Is the FindControl returning a control? Is the label in the final markup?


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Hi, yep stepped through the code and it got there fine (the stepping through the html part doesn't work too great for me). It also found the label ok. However it did not make it visible.
 
What does the final markup look like? i.e. can you see the rendered label?


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Hi, here's the html part:

Code:
<%@ Page Language="C#" MasterPageFile="~/Template.master" AutoEventWireup="true" CodeFile="New.aspx.cs" Inherits="MyAppNamespace.UI.Admin.Admin_Users_New" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server">
    <h1>New User</h1>
    <asp:FormView ID="fvwUser" runat="server" DataSourceID="objUser" DefaultMode="Insert" Width="100%">
        <InsertItemTemplate>
            <p align="center">Fields marked with a <img src="/Admin/Images/Global/Star.gif" alt="Required" /> must be completed.</p>
            <p align="center"><asp:Label ID="lblSummary" runat="server" CssClass="normalRed" Visible="false"></asp:Label></p>
            <asp:ValidationSummary ID="valSummary" runat="server" />
            <table border="0" cellpadding="3" cellspacing="1" width="100%">
                <tr>
                    <td class="tableHeader"><asp:Label runat="server" ID="lblUserName" AssociatedControlID="txtUserName" Text="Username:" /></td>
                </tr>
                <tr>
                    <td class="tableContent">
                        <asp:TextBox ID="txtUserName" Text='<%# Bind("UserName") %>' runat="server"></asp:TextBox> <img src="/Admin/Images/Global/Star.gif" alt="Required" />
                        <asp:RequiredFieldValidator ID="valRequireUserName" runat="server" ControlToValidate="txtUserName" ErrorMessage="Username is required" ToolTip="Username is required">*</asp:RequiredFieldValidator>
                    </td>
                </tr>
                <tr>
                    <td class="tableHeader"><asp:Label runat="server" ID="lblPassword" AssociatedControlID="txtPassword" Text="Password:" /></td>
                </tr>
                <tr>
                    <td class="tableContent">
                        <asp:TextBox ID="txtPassword" Text='<%# Bind("Password") %>' runat="server"></asp:TextBox> <img src="/Admin/Images/Global/Star.gif" alt="Required" />
                        <asp:RequiredFieldValidator ID="valRequirePassword" runat="server" ControlToValidate="txtPassword" ErrorMessage="Password is required" ToolTip="Password is required">*</asp:RequiredFieldValidator>
                    </td>
                </tr>
            </table>
            <p align="right"><asp:Button ID="btnCancel" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" CssClass="passiveButton" /> <asp:Button ID="btnInsert" runat="server" CausesValidation="True" CommandName="Insert" Text="Insert" CssClass="activeButton" /></p>
        </InsertItemTemplate>
    </asp:FormView>
    <asp:ObjectDataSource ID="objUser" runat="server" InsertMethod="InsertUser" TypeName="MyAppNamespace.BLL.Users.Users" OnInserted="objUser_Inserted">
        <InsertParameters>
            <asp:Parameter Name="UserName" Type="String" />
            <asp:Parameter Name="Password" Type="String" />
        </InsertParameters>
    </asp:ObjectDataSource>
</asp:Content>

and here's is the code behind file:

Code:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MyAppNamespace;

namespace MyAppNamespace.UI.Admin {
    public partial class Admin_Users_New : System.Web.UI.Page {
        protected void Page_Load(object sender, EventArgs e) {

        }

        protected void objUser_Inserted(object sender, System.Web.UI.WebControls.ObjectDataSourceStatusEventArgs e) {
            if ((bool) e.ReturnValue == true) {
                Response.Redirect("~/Admin/Users/");
            } else {
                Label lblSummary = (Label) fvwUser.FindControl("lblSummary");
                lblSummary.Text = "Error: Username already exists";
                lblSummary.Visible = true;
            }
        }
    }
}

Hope this helps.
 
When i hit submit none of the viewstate has been remembered and the part of the page where i have the label control renders as the following:

<p align="center">Fields marked with a <img src="/Admin/Images/Global/Star.gif" alt="Required" /> must be completed.</p>
<p align="center"></p>

This seems strange to me.
 
jgd123456,

<p align="center">Fields marked with a <img src="/Admin/Images/Global/Star.gif" alt="Required" /> must be completed.</p>
<p align="center"></p>

You see an empty <P> tag because the label is set to Visible = False which means in a sense it isnt there (Ive ran into this before). What I did to rectify the paoblem is this:

1) Created a CSS class and set its DISPLAY to NONE
2) Applied the new style to my label
3) When I wanted the label to show I added:
Code:
lblYourLabelName.Attributes.Add("DISPLAY","BLOCK")
and that worked for me. Im not saying this will work for you (circumstances might be different) but its worth a try.

Hope this helps!



Senior Qik III, ASP.Net, VB.Net ,SQL Programmer

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"In the new millennium there will be two kinds of business; those on the net and those out of business"

~ Bill Gates ~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top