Solved

Invoking the Details View "Item Inserting" event

Posted on 2010-11-27
14
1,539 Views
Last Modified: 2012-05-10
I  have invoked the Modal Pop up extender, from the "Save" button of the Details View(insert mode) control.
 Now I need to invoke the  DetailsView "Item Inserting" event From the "Send" button on the Modal pop up extender.
Previously,this event was being invoked automatically from the "Save" button.


Urgently required

0
Comment
Question by:Ammar Iqbal
  • 10
  • 4
14 Comments
 
LVL 3

Assisted Solution

by:abdkhlaif
abdkhlaif earned 500 total points
ID: 34221471
Call InsertItem method which will fire the Item Inserting event for you

protected void Button3_Click(object sender, EventArgs e)

{

	DetailsView1.InsertItem(true);

}

Open in new window

0
 
LVL 1

Author Comment

by:Ammar Iqbal
ID: 34221573
Hi,
Thanks for solution. I have invoked the Details View Item Inserting event handler by DEtailsView1.InsertItem(false) --where false indicates the causevalidation.
If we keep it true then we are getting the  un handled Http Exception which is Page.IsValid can not be called before validation has taken place.

Secondly,I have invoked the item inserting event handler from this line as well:
DetailsView1.ItemInserting+=new DetailsViewInsertEventHandler(DetailsView1_ItemInserting);

For this ,I need to set the CommandName  to  "Insert "in the asp.Button  control property (.aspx file)

I have small bit of doubt for one thing. I need to hide the modal pop up after the record has been inserted.
Here is the flow of this event handler: First of all there is an email functionality, then then ItemINserting event is invoked, after that when the record is successfully added to db, I need to hide the Modal pop up dialog.
What should I do to hide it. Since the modal pop is in the details view control, I can not access its "id" in the code behind. So I can not call the hide method directly.
0
 
LVL 1

Author Comment

by:Ammar Iqbal
ID: 34221863
is anyone there?
0
 
LVL 3

Expert Comment

by:abdkhlaif
ID: 34224926
You can access any control on your page from the code using the Page.FindControl method:
using AjaxControlToolkit;

...

ModalPopupExtender mpe = (ModalPopupExtender)Page.FindControl("ModalPopupExtender1");

mpe.Show();

mpe.Hide();

Open in new window

0
 
LVL 1

Author Comment

by:Ammar Iqbal
ID: 34225030
Hi,
My Details View item inserting event is being invoked on using (DetailsView1.InsetITem(false). and I can notice that my record is also being inserted, but some how when I add the email functioanity  afterthis line, or in some instances  if I do n't enter email functionality  and only do

Details View1.InsertItem(false). I am getting the following MS J sCript run time error. It is saying that page validation is being already perofmred,
But see the last two lines in the following eerror .I am certain that data which is inserting is valid,and item inserting  event handler is also being invoked properly. In the last 2 lines, it is saying i neeed to register the post bacl or callback validation.

Please have a look at it on an urgent basis

Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerServerErrorException: Invalid postback or callback argument.  Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page.  For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.

 If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.
0
 
LVL 3

Expert Comment

by:abdkhlaif
ID: 34225145
attach the code for DetailsView1_ItemInserting
I need to see why you have to invoke Item Inserting event
0
 
LVL 1

Author Comment

by:Ammar Iqbal
ID: 34225178
This is what I am doing .
<asp:LinkButton ID="i_insert" runat="server" CausesValidation="False" CommandName="Insert" CssClass="button" Visible="<%# IsAdmin %>">

<span class="ui-icon ui-icon-check"></span>Save

</asp:LinkButton>

-------------------------------------------------------------------





<act:ModalPopupExtender ID="mpUserNotification"  BehaviorID="mpUserNotificationBehavior" runat="server" TargetControlID="i_insert" BackgroundCssClass="modalPopup" 

                                             PopupControlID="divPopup" DropShadow="true" >

</act:ModalPopupExtender>

Open in new window

protected override void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)

        {

            try

            {

                if (e.Values.Count == 0)

                {

                    fillDataSource();

                    DataView dv = DataSource;

                    DataTable dt = dv.Table;

                    int count = dv.Count;



                    DataRow dr = dt.NewRow();

                    foreach (DataColumn dc in dt.Columns)

                    {

                        object value = getValue(DetailsView1, dc);//, DetailsViewMode.Insert

                        if (value != DBNull.Value || dc.AllowDBNull)

                        {

                            if (dc.ColumnName == "id")

                            {

                                count = count + 1;

                                value = count;

                            }

                            if (dc.ColumnName == "datetime")

                            {

                                DateTime current = DateTime.Now;

                                value = current;

                            }

                           /*

                            if (dc.ColumnName == "image")

                            {

                                string file=value.ToString();

                                value = System.IO.Path.GetFullPath(file);

                            }

                            */

                            if (dc.ColumnName == "instrument")

                            {

                                if (value == "")

                                {

                                    value = DBNull.Value;

                                }

                                else

                                {

                                    

                                    DataSet dsSensorId = new DataSet();

                                    dsSensorId = getSensorName(value.ToString());

                                    object id = dsSensorId.Tables["Instrument"].Rows[0].ItemArray[0].ToString();

                                    value = id;

                                }

                            }

                          

                            dr[dc] = value;

                        }

                     }

                    dt.Rows.Add(dr);

                    int ct = dt.ParentRelations.Count;

                    addMessage(dataprovider.updateTable(dr.Table),0);

                    //updateTable(dr.Table);

                }



            }

            catch (SqlException sex)

            {

                if (sex.Number == 2601) addMessage(addMessage("This name is already taken!", 1));//se.Number==2160

                else addMessage(addMessage(sex));

            }//end try2_catch2

            catch (Exception ex)

            {

                addMessage(addMessage(ex));

            }//end try2_catch2



        }

Open in new window

protected void btnSendEmail_Click(object sender,EventArgs e)

        {

            //DetailsView1.ItemInserting+=new DetailsViewInsertEventHandler(DetailsView1_ItemInserting);

            DetailsView1.InsertItem(false);

            

            string[] toaddress = null;

            string strtoaddresses = string.Empty;

            ListBox ToList = (ListBox)DetailsView1.FindControl("listTo");

            if (ToList != null)

            {

                if (ToList.Items != null)

                {

                    int listcount = ToList.Items.Count;

                    toaddress = new string[listcount];

                    for (int i = 0; i < listcount; i++)

                    {

                        toaddress[i] = ToList.Items[i].ToString();

                    }



                    //making a semi colon separated string

                    for (int i = 0; i < listcount; i++)

                    {

                        if (i < listcount - 2) strtoaddresses += toaddress[i] + ',';

                        if (i == listcount - 1) strtoaddresses += toaddress[i];

                    }



                    MailMessage mailMsg = new MailMessage();

                    MailMessageEventArgs me = new MailMessageEventArgs(mailMsg);

                    SmtpClient smtpClient = new SmtpClient();

                    mailMsg.From = me.Message.From;

                    mailMsg.To.Add(strtoaddresses);

                    mailMsg.Subject = "Test Email Log Module";

                    mailMsg.IsBodyHtml = true;

                    mailMsg.Body = " Introducing the Cautus Geo's  Munin Log Module";

                    try

                    {

                        smtpClient.EnableSsl = true;

                        smtpClient.Send(mailMsg);



                    }

                    catch (Exception ex)

                    {

                        System.Console.WriteLine(ex.Message);



                    }

                   

                }





            } 

         

       ScriptManager.RegisterStartupScript(Page, this.GetType(), "ClosePopup", "<script>$find('mpUserNotificationBehavior').hide();</script>", false);



        

        }

Open in new window

0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 1

Author Comment

by:Ammar Iqbal
ID: 34225186
If i am not using the modal pop up extender, then DEtails View item inserting event handler is being automatically invked from the savew button. Now its' being invoked from the button on the post back.I ned to insert recrod, ,send email to different users, and hide the dialog...

I am able to insert the record, but some howthis run time jscript error is coming when I am introducing the email functioanlity .
0
 
LVL 1

Author Comment

by:Ammar Iqbal
ID: 34225251
are uthere?
0
 
LVL 3

Accepted Solution

by:
abdkhlaif earned 500 total points
ID: 34225321
in the .aspx page, make sure you have this code for your DetailsView control:
OnItemInserting="DetailsView1_ItemInserting"

and remove this line from btnSendEmail_Click:
DetailsView1.InsertItem(false);

then, move the email functionality to DetailsView1_ItemInserting
0
 
LVL 1

Author Comment

by:Ammar Iqbal
ID: 34225636
Areu sure.I donæt have this  on my .aspx page.
Because when I was invoking it on the Save button in a normal way visa "Save" button. I do not require this.

In this case when i want to invoke item inserting  via the button on a modal pop up? Do you think,i need to do this..?
0
 
LVL 1

Author Comment

by:Ammar Iqbal
ID: 34225769
same problem.... I have removed  DEtailsView1.insertItem form btnSend, Record is not inserted andsame error is coming
0
 
LVL 1

Author Comment

by:Ammar Iqbal
ID: 34225866
Somehow I got hold of hat was wrong in it.. Somehow, in my db table total numbr of records were 21,  and in the primary key column the id of the first record was 22, ,This id was subsequently increased by 1 as it traverses through the rest of the 21 records....
When ever i  was trying to insert a new record,  the id of the record should be assigned 22, but that is alredy assigned, and it was raising the Exception. that is why it was not inserting,and i was pressing the send button again and agian , thus the java script run time erorr of postbacks and other stuff....
now its solved, but it is inserting 3 records at a time, i need to look into m
0
 
LVL 1

Author Comment

by:Ammar Iqbal
ID: 34225871
i need to look into my logic, whats wrong in it.  Thanks for looking into it.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now