Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Invoking the Details View "Item Inserting" event

Posted on 2010-11-27
14
Medium Priority
?
1,664 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 4
14 Comments
 
LVL 3

Assisted Solution

by:abdkhlaif
abdkhlaif earned 2000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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
 
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 2000 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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

604 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